Backwards_Dave
Backwards_Dave

Reputation: 509

C array element initialization

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?

Upvotes: 2

Views: 238

Answers (3)

waitingkuo
waitingkuo

Reputation: 93964

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.

Upvotes: 0

Tudor
Tudor

Reputation: 62469

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

Upvotes: 2

Jay
Jay

Reputation: 24905

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.

Upvotes: 3

Related Questions