简体   繁体   中英

Strange values on array initialization

I'm new to programming in C and i found something i don't understand:

When initializing an array without given values, i thought all elements would be zero. I wrote this few lines of code...

int main()                    
{     
    int obj[10][4];

    for (int a = 0; a < 10; a++)
    {
        print("%d\t%d\t%d\t%d\n", obj[a][0], obj[a][1], obj[a][2], obj[a][3]);
    }            
}

...and was pretty confused by its output:

0       0       0       0
0       0       0       0
0       0       0       0
0       0       0       0
0       0       0       0
0       0       0       0
0       0       0       0
0       0       0       7661
7960    2697    2260    7960
1551630361      -2130960380     146780176       -2130960380

I don't understand why some of the values are zero and some are not. I was even more confused that this numbers change when i add more code. For example, i changed the previous example and added another print() ...

int main()                    
{     
    int obj[10][4];

    print("start\tstop\tcenter\tdist\n\n");

    for (int a = 0; a < 10; a++)
    {
        print("%d\t%d\t%d\t%d\n", obj[a][0], obj[a][1], obj[a][2], obj[a][3]);
    }            
}

...getting this:

start   stop    center  dist

0       0       0       0
0       0       0       0
0       0       0       0
0       0       0       0
0       0       0       0
0       0       0       0
0       0       0       0
0       0       0       7673
7972    2709    2272    7972
1551630361      -2130960380     146780176       -2130960380

Using a bigger array, this numbers are not only found at its end. The first few values are always zero, but then "something" happens.

I've found a solution here at SO , using memset() , that works for me, but... what's going on here?

Could someone explain this, using words a C-newbie would understand?

Simply put, you can not assume the array is initialized to 0 unless you explicitly made the initialization yourself. The C standard does not guarantee anything about the content of the array, just that you'll have memory allocated for it.
A great reference on initializing and arrays in C general can be found here: http://www.tutorialspoint.com/cprogramming/c_multi_dimensional_arrays.htm

You have a declaration of variable but no initialization.

If you want your array to be zero initialized simply put this:

int obj[10][4] = {0};

Initial values of anything are undefined. Your code just defines an array - it never assigns any value to what's in it.

storage class type of int obj[10][4] is auto and it is stored on stack. auto variables don't get initialized values, ie they have garbage values.If you want array to initialize with zeroes then make it global ie declare array outside the main() function.

the stack contains trash until the program places specific values into variables defined on the stack. The startup code does not initialize the stack contents.

Note: variables in the global space/file global space are initialized by the startup code. where is no specific initializer is specified, then the memory is set to 0x00.

Initially, the array will have some garbage values or any unpredictable values. There is no telling what these values will be or where will they be located in the array.

Whenever you declare an array in C, initialize it before using. ie, run a loop to initialize all the array locations to zero or any number that you want to store. That way you will know for certain what the array contains and in further processing, your array will not show undefined behaviour.

Not an expert but a hypothesis from experience:

If you initialize an array as a static int myArray[1000] , then C allocates new memory filled with 1000 zeros for the duration of the program. Initializing with int myArray[1000] just sets aside 1000 registers, some of which may contain old values from a previous use of the register.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM