Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is an undefined vector element represented as in memory?

I'm currently studying C and I have a question in which I have to guess what values are in the memory. When I say, for example long var1[1243] = {10, 1, 0, -1, -10} and define this vector, the first 5 elements of it are already defined, but the allocated bytes that represent the remaining 1238 elements, what are they set as in memory? More specifically, are they 0? Would saying, in assembly, after defining them, .zero <number of remaining bytes in vector> correct?

like image 664
zeval Avatar asked Dec 05 '25 03:12

zeval


1 Answers

By having at least some elements initialized {10} C will initialize all the remaining elements in the array the same as a global variable. Either numeric zero or null pointer depending on type, and in the case of arrays of structs, all members of the struct, recursively.

In C++ an empty initializer also works long var1[1243] = {}, but that is not allowed in C.

like image 130
Fire Lancer Avatar answered Dec 07 '25 20:12

Fire Lancer