Lifetime : storage duration which an object has by the standard
Segments : Regions divided into a programs's address space
The compiler decides on a segment for each object based on its lifetime
The linker (final complier phase) groups all the program's objects by segment
Finally, when a program runs, the operating system loads the segements into memory ( The stack and heap segments grow on demand )
| Object decalaration | Lifetime | Segment | Example address range |
|---|---|---|---|
| Constant global | Static | Code(or text) | 0x40'0000(similar to 1 * 2^22) |
| Global | Static | Data | 0x60'0000(similar to 1.5 * 2^22) |
| Local | Automatic | Stack | 0x7fff'448d'0000(similar to 2^47 = 2^25 * 2^22) |
| Anonymous, returned by new | Dynamic | Heap | 0x1a0'0000(similar to 8 * 2^22) |
Constant global data and global data have the same life time but are stored in different segments (The operating system uses different segments so it can prevent the program from modifying constatnts)
An executable is normally at least as big as the static-lifetime data
All that data must be in memory for the entire lifetime of the program, it's written to disk and then loaded by the OS before the program starts running
bss segment is used to hold modifiable static-lifetime data with initial value zero