Originally Posted by
matsp
The linked PDF has several strange things in it. The very first sentence doesn't make ANY sense to me.
It has got a picture that shows how the different sections may be arranged. But bear in mind that for example in Linux the stack is at a higher address than the heap [and the stack is at the "highest available address"], rather than at a lower address as depicted in the linked PDF, and there is absolutely nothing saying that the data section can't be before the code section for example [not that I have seen such a setup other than in systems where the code is in ROM at a higher address].
The sentence "this is how a process is placed into its virtual address space"
could perhaps mean "this is the placement of the different elements that make up
a process (a running program in memory), built from the information in the executable
file, such as how much stack space and static data space to reserve, the actual code
and initialized data, etc."
The term "virtual address space" means that your program "thinks" its in a big address
space (along with shared components, like dll's), instead of the real RAM address space.
The OS/CPU map virtual to real addresses, even swapping pieces of code/data in and
out, generally back to different real addresses but always mapped to the same virtual
address.
The only reason I can think of for having the code at the bottom is to
ensure that 0 is not a valid data address. Also, since the code doesn't
grow, it should be right up against the bottom or top of the available virtual address space.
Since both the data and stack segments also do not grow, they should be placed
either up against the code or up against the top of memory.
The heap then simply starts at the top or bottom of what's left.
The stack or heap can both grow either way.
The following code on my PC/XP produces confusing results.
It looks like dynamics and locals are below the code!
Look at the distance between the two globals (16 bytes for 4-byte values).
Look at the distance between the dynamics!
What are the results for your Linux machine?
Code:
/* Output on PC/XP
* global1 global2 local1 local2 dynamic1 dynamic2 function
* 00403030 00403020 0022ff8c 0022ff88 003d3dd8 003d3e38 00401334
*/
#define mint() ((int*) malloc( sizeof( int )))
int global1;
int global2;
int main() {
int local1;
int local2;
int *dynamic1 = mint();
int *dynamic2 = mint();
printf( "global1 global2 local1 local2 dynamic1 dynamic2 function\n" );
printf( "%08x %08x %08x %08x %08x %08x %08x\n",
&global1, &global2, &local1, &local2, dynamic1, dynamic2, main );
}