The output of your program is correct, because
- Neither
malloc
norrealloc
have anything to do with the automatic storage (i.e. "the stack"). They allocate memory from the dynamic storage area (i.e. "the heap"). One should not expect the position of the top of the stack to change in response to calls tomalloc
,realloc
, or for that matter, any other function. - The value of
sizeof(arr)
does not depend on what you have allocated to it. It is computed at compile time, and it is always equal to the size of a pointer. On your system, pointers use 8 bytes. malloc
often gives you more memory that you ask, and stores the actual value in a special location thatrealloc
can access at a later time. If yourealloc
down, orrealloc
within the bounds, the value returned byrealloc
does not change. That's the reason why it may perform better than simply callingmalloc
andmemcpy
.