I was looking at understanding how local variables are allocated memory in C. Based on this, the array will be created on the stack. And I thought the stack addressing starts from a higher address and then goes to a lower address. So say I had this:
int a;
int arr[3];
Say a
was at address 100. Then arr
would be at address 96
(100 - 4), with the address for arr[3]
at 88 (96 - 2 * 4), since int will take 4 bytes.
But in reality, I see something very different happening. If I make an arr of size 1 then it works as expected. But if I increase the array size then the addresses look very different.
It seems like an array of size > 1 does not go on the stack but somewhere else (heap?). Can someone explain to me the gap in addresses between a
and arr
for size > 1?
Size 1 arr |
---|
|
|
Size 2 arr |
|
|