简体   繁体   中英

need explanation of how memory address work in this C program

I have a very simple C program where I am (out of my own curiosity) investigating which memory addresses are used to allocate local variables. My program is:

#include <stdio.h>

int main()
{
  char buffer_1[8], buffer_2[8], buffer_3[8];
  printf("address of buffer_1 %p\n", buffer_1);
  printf("address of buffer_2 %p\n", buffer_2);
  printf("address of buffer_3 %p\n", buffer_3);
  return 0;
}

output is as follows:

address of buffer_1 0x7fff5fbfec30
address of buffer_2 0x7fff5fbfec20
address of buffer_3 0x7fff5fbfec10

my question is: why do the address seem to be getting smaller? Is there some logic to this? thank you.

The compiler is allowed to do whatever it wants with your automatic variables. In this case it just looks like it's putting them consecutively on the stack. On most popular systems in use today, stacks grow downwards.

Most compilers allocate stack memory for local variables in one step, at the very beginning pf the function. The memory is allocated as a single continuous block. Under these circumstances, the compiler, obviously, is free to use absolutely any memory layout for local variables inside that block. If can put them there so that the addresses increase in the order of declaration. Or decrease. Or arranged randomly. It is an implementation detail. And there's not much logic behind it.

It is quite possible that in your case the compiler tried to "pretend" that the memory for the arrays was allocated in the stack sequentially and independently (even though that was not the case). If on your platform stack grows downwards (as it does on many platforms), then it is expected that object declared later will have smaller addresses.

But again, functions don't allocate local objects individually. And on top of that the language makes no guarantees about any relationships between local object addresses. So, there's no real reason to prefer one ordering over the other.

The output of your C program is platform-dependent, compiler-dependent. There cannot be just one perfect answer because the address arrangements vary based on: Whether the system is little or big endian. What kind of OS you are compiling on. What kind of memory architecture you are compiling for. What kind of compiler you are using(and compilers might have bugs too) Whether you are on 64-bit or 32-bit platform. And so much more.

But most important of all, is the type of processor architecture. :)

Here is a list of stack growth strategies per processor:

x86,PDP11    Downwards
System z     In a linked list fashion, downwards, mostly.
ARM          Select-able and can grow in either up or downward.
Mostek6502   Downwards (but only 256 bytes).
SPARC        In a circular fashion with a sliding window, a limited depth stack. 
RCA1802A     Subject to SCRT(Standard Call and Return Technique) implementation.

But, in general, your compiler, at compile-time should map those addresses into the binary file generated. Then at the run-time, the binary file may occupy(or may pretend to occupy) a sequential set of memory addresses. And in your case the addresses printed by your C source, show that the stack is growing downward.

Basically compiler has responsibility to allocate memory to all the variables . Array gets address on stack. but it has nothing to do with the o/p you are getting. Basically The thing is compiler found the contiguous space(or chunk of memory) empty at that time and hence it allocated it to your program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM