简体   繁体   中英

Difference between two different array initialisation in C

Between the

int array[100][100];

And

int array[100][100]={0};

In the first one, when I print all the elements of the array then after 94th row, halfway through it, I start to get the garbage values but all the values before that are 0, whereas for the second one, all the values are 0.

Does not the first declaration too initialize with a default 0 value, and if it does not, howcome not all of the values in the array are garbage and why only after 94th row the garbage values are appearing?

Does not the first declaration too initialize with a default 0 value?

No. Assuming this is a locally scoped variable, your first declaration does not result in any initialization at all.

How come not all of the values in the array are garbage and why only after 94th row the garbage values are appearing?

Uninitialized memory can have any value, including the values of 0 that you are observing.

The first style will cause data to be initialized to 0 only if the variable is a global. Local variables that lack explicit initialization will be equal to whatever happened to be in their storage location when the space was assigned to the variable.

The first declaration does not guarantee any initialization, in fact i do not think it performs any.

The fact you have clear memory until 94th element is because the OS gave you some clean / yet unused memory.

If you want to be sure to have clear memory you have to use the second declaration

There's no explicit initialization in the first statement. An implicit initialization is possible, if the array is declared in global scope.

The reason of "seems to be half initialized" is that a user space program typically starts with zeroed stack / heap for security reasons (not accessing data from other processes containing passwords etc.), but there's no guarantee that a local variable is always cleared. There's rather a guarantee that is it not because of performance reasons. Even in C there can be a lot of activity happening before main is called: crt0 initializes the runtime, copies static data from read_only sections to global variables etc. The result being that a lot of programs heap and stack are no longer intact.

int array[100][100];

  if this declaration is in the scope of main i.e in Stack Segment then

memeory for this array will be alloacted in stack. Consider if no one used this stack you may get the value zero otherwise it will garbage,its not particular to 94th row elements.

 if same declaration is in Global, then all the array elements will be

instialized to Zero.

int array[100][100]={0};

 Irrespective of any segments [Stack/Data(Global,Static)] if any one 

variable got intialized in an array,then rest of the array elements will be intialized to Zero.

First of all +1 for the clean and straight forward question. Now to the answer, the local variables if not explicitly initialized, are not initialized implicitly. All the 93 members containing 0 and not some other garbage value is not a defined behavior. You can try that with doing the above piece of code multiple times and you will see different garbage values returning back for the first declaration. For a better understanding, step into the assembly level of the above code and see for yourself what happens internally.

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