Organization of Virtual Memory in C

For each of the following, where does it appear to be stored in memory, and in what order: global variables , local variables , static local variables , function parameters , global constants , local constants , the functions themselves (and is main a special case?), dynamically allocated variables .

How will I evaluate this experimentally,ie, using C code?

I know that
global variables -- data
static variables -- data
constant data types -- code
local variables(declared and defined in functions) -- stack
variables declared and defined in main function -- stack
pointers(ex: char *arr,int *arr ) -- data or stack
dynamically allocated space(using malloc,calloc) -- heap


You could write some code to create all of the above, and then print out their addresses. For example:

void func(int a) {
    int i = 0;
    printf("local i address is %xn", &i);
    printf("parameter a address is %xn", &a);
}

printf("func address is %xn", (void *) &func);

note the function address is a bit tricky, you have to cast it a void* and when you take the address of a function you omit the (). Compare memory addresses and you will start to get a picture or where things are. Normally text (instructions) are at the bottom (closest to 0x0000) the heap is in the middle, and the stack starts at the top and grows down.


In theory

Pointers are no different from other variables as far as memory location is concerned.

Local variables and parameters might be allocated on the stack or directly in registers.

constant strings will be stored in a special data section, but basically the same kind of location as data.

numerical constants themselves will not be stored anywhere, they will be put into other variables or translated directly into CPU instructions.
for instance int a = 5; will store the constant 5 into the variable a (the actual memory is tied to the variable, not the constant), but a *= 5 will generate the code necessary to multiply a by the constant 5.

main is just a function like any other as far as memory location is concerned. A local main variable is no different from any other local variable, main code is located somewhere in code section like any other function, argc and argv are just parameters like any others (they are provided by the startup code that calls the main ), etc.

code generation

Now if you want to see where the compiler and runtime put all these things, a possibility is to write a small program that defines a few of each, and ask the compiler to produce an assembly listing. You will then see how each element is stored.

For heap data, you will see calls to malloc, which is responsible for interfacing with the dynamic memory allocator.

For stack data, you will see strange references to stack pointers (the ebp register on x86 architectures), that will both be used for parameters and (automatic) local variables.

For global/static data, you will see labels named after your variables.

Constant strings will probably be labelled with an awful name, but you will notice they all go into a section (usually named bss) that will be linked next to data.

runtime addresses

Alternatively, you can run this program and ask it to print the addresses of each element. This, however, will not show you the register usage.

If you use a variable address, you will force the compiler to put it into memory, while it could have kept it into a register otherwise.

Note also that the memory organization is compiler and system dependent. The same code compiled with gcc and MSVC may have completely different addresses and elements in a completely different order.

Code optimizer is likely to do strange things too, so I advise to compile your sample code with all optimizations disabled first.
Looking at what the compiler does to gain size and/or speed might be interesting though.

链接地址: http://www.djcxy.com/p/14476.html

上一篇: 动态分配内存存储说明

下一篇: 在C中组织虚拟内存