Advanced Programming in the UNIX Environment, Second Edition (Addison-Wesley Professional Computing Series)
|
7.6. Memory Layout of a C Program
Historically, a C program has been composed of the following pieces:
Figure 7.6 shows the typical arrangement of these segments. This is a logical picture of how a program looks; there is no requirement that a given implementation arrange its memory in this fashion. Nevertheless, this gives us a typical arrangement to describe. With Linux on an Intel x86 processor, the text segment starts at location 0x08048000, and the bottom of the stack starts just below 0xC0000000. (The stack grows from higher-numbered addresses to lower-numbered addresses on this particular architecture.) The unused virtual address space between the top of the heap and the top of the stack is large. Figure 7.6. Typical memory arrangement
Several more segment types exist in an a.out, containing the symbol table, debugging information, linkage tables for dynamic shared libraries, and the like. These additional sections don't get loaded as part of the program's image executed by a process. Note from Figure 7.6 that the contents of the uninitialized data segment are not stored in the program file on disk. This is because the kernel sets it to 0 before the program starts running. The only portions of the program that need to be saved in the program file are the text segment and the initialized data. The size(1) command reports the sizes (in bytes) of the text, data, and bss segments. For example: $ size /usr/bin/cc /bin/sh text data bss dec hex filename 79606 1536 916 82058 1408a /usr/bin/cc 619234 21120 18260 658614 a0cb6 /bin/sh
The fourth and fifth columns are the total of the three sizes, displayed in decimal and hexadecimal, respectively. |
|