2

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 3

3

These are just those values which are already present in the memory where the space for the array is allocated. So, there is no "determination" going on here.

Sign up to request clarification or add additional context in comments.

Comments

2

Technically it's undefined behavior to read uninitialized variables. They can be anything, ranging from leftover memory junk to compiler predefined values.

3 Comments

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.
@Jens Gustedt: Indeed, there seems to be a debate in other related topics about what to call this situation. Suggestions?
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
0

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

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.