Vergil333 Vergil333 - 3 months ago 8
C++ Question

Default values of array

I am curious. How are those undefined values trowed over here when I try to print them?

I thought that an undefined array has values equal to 0.

Code:

#include <iostream>
#include <stdio.h>
using namespace std;

int main() {
// your code goes here

int log[][4];
int addOne=0;

for(int i=0;i<7;i++)
{
log[addOne][0]=11+i;
log[addOne][1]=11+i;
//log[addOne][2]=11+i;
log[addOne++][3]=11+i;
}

for(int i=0;i<11;i++)
{
printf("log[%d][0] = %d\n", i, log[i][0]);
printf("log[%d][1] = %d\n", i, log[i][1]);
printf("log[%d][2] = %d\n", i, log[i][2]);
printf("log[%d][3] = %d\n", i, log[i][3]);
}

return 0;
}


Output:

log[0][0] = 11
log[0][1] = 11
log[0][2] = 1
log[0][3] = 11
log[1][0] = 12
log[1][1] = 12
log[1][2] = -1217403463
log[1][3] = 12
log[2][0] = 13
log[2][1] = 13
log[2][2] = -1081953076
log[2][3] = 13
log[3][0] = 14
log[3][1] = 14
log[3][2] = -1081953088
log[3][3] = 14
log[4][0] = 15
log[4][1] = 15
log[4][2] = -1216726312
log[4][3] = 15
log[5][0] = 16
log[5][1] = 16
log[5][2] = -1219603196
log[5][3] = 16
log[6][0] = 17
log[6][1] = 17
log[6][2] = 6
log[6][3] = 17
log[7][0] = -1216729360
log[7][1] = -1216729468
log[7][2] = -1081952888
log[7][3] = -1217105850
log[8][0] = -1216726312
log[8][1] = -1216764868
log[8][2] = -1216758952
log[8][3] = 0
log[9][0] = -1217100393
log[9][1] = -1216548864
log[9][2] = -1216546512
log[9][3] = 1
log[10][0] = -1081952912
log[10][1] = -1216623457
log[10][2] = -1216546072
log[10][3] = -1217997256


Is there a way to initialize 0 value to every index?

Answer

Very simple

int log[][4] = {{0}};

But then your code would be terribly wrong, because you didn't allocate enough space for so many values1 and thus, it will invoke undefined behavior.

If you want that, you need

#include <stdio.h>

int
main(void)
{
    int values[11][4] = {{0}};
    int addOne = 0;

    for (int i = 0 ; i < 7 ; i++) {
        values[addOne][0] = 11 + i;
        values[addOne][1] = 11 + i;
        values[addOne++][3] = 11 + i;
    }

    for (int i = 0 ; i < 11 ; i++) {
        printf("values[%d][0] = %d\n", i, values[i][0]);
        printf("values[%d][1] = %d\n", i, values[i][1]);
        printf("values[%d][2] = %d\n", i, values[i][2]);
        printf("values[%d][3] = %d\n", i, values[i][3]);
    }
    return 0;
}

and don't use log as an identifier because it's the natural logarithm function from math.h header.

Uninitialized2 values are not automatically initialized to 0 except if they are declared static, or when they are in the global scope. It is done that way for performance reasons, initializing the values to 0 to overwrite the zeroes takes unnecessary work and you should be thankful that you will onlye initialize the values luckily with the final value.

There is a function, in the strings.h header that can do this too but it's rarely useful for something other than 0 except, for characters arrays. That is because you can set any number of bytes, thus you can't set the value 1 to every element in an array of ints with such function, it is

void *memset(void *s, int c, size_t n);

despite the second parameter being an int it can only set bytes, exactly n bytes.

As important is to mention, that as comments on your question by @user3386109 here and @NathanOliver here pointed out you can't really do this

int values[][4];

because that way it would be impossible to know what the size of the array is. You should either specify the missing size or, add an initializer like I did at the beginning of this answer. This is as a result and of course, illegal syntax.


1as it is it's a size 1 array of arrays of size 4

2not undefined, that's a different thing