If you don't initialize elements in an array, you can see they are allocated a random large number when you debug. Just wondering how this number is determined?
3 Answers
Technically it's undefined behavior to read uninitialized variables. They can be anything, ranging from leftover memory junk to compiler predefined values.
3 Comments
Jens Gustedt
No, it is not undefined behavior. It is only undefined behavior if those values correspond to a trap representation for the type. I don't know of any of the current architectures that has trap representations for integer types.
Tudor
@Jens Gustedt: Indeed, there seems to be a debate in other related topics about what to call this situation. Suggestions?
Jens Gustedt
C11 has this: indeterminate value either an unspecified value or a trap representation, and unspecified value valid value of the relevant type where this International Standard imposes no requirements on which value is chosen in any instance
When you declare an array, the stack pointer will be added , and then return the address of the first element. That's all, data in memory will not be changed.
1 Comment
Backwards_Dave
Your answer doesn't really have anything to do with what I was asking. I was asking how the random data gets in (or how it is determined) the uninitialized array elements