Operating Systems · Process Concept and States
Process Memory Layout: Code, Data, Heap and Stack
Overview
When a program is loaded to run, the operating system creates a structure for it in main memory that holds everything the process needs; this whole region is called the process boundaries. The layout of this region — code, static/global variables, heap and stack — determines what a process can access and why accessing anything outside it causes a segmentation fault.
Key concepts
- Process boundaries: the entire memory structure the OS creates for a process in main memory. Everything the process requires lies inside its own boundary, and the OS must never let the process cross it.
- Executable code: the .exe file / a.out file that was produced and is to be executed — the code the CPU actually runs.
- Static variables: variables that are created once and then remain forever throughout the lifetime of the process; they are not created and destroyed again and again — there is exactly one copy for the whole run.
- Global variables: a kind of static variable that must be accessible to everything in the process; like static variables they are created once and exist forever, so they stay in the same region for the whole life of the process.
- Heap: space for dynamic memory allocation (malloc, calloc).
Because we do not know in advance what requirement we might get, data structures are not declared unless required, so a process needs space that will be used later, at the time it is needed. - Stack (function stack): the part every process must contain; its advantage is for recursion calls and function calling, which is why it is also called the function stack.
- Segmentation fault: the result of any attempt to access memory outside the process boundary.
How it works
- The executable (.exe / a.out file) is placed inside the process structure in main memory — this is the code the CPU will run.
- Static variables are placed above the code; they are created once and remain forever throughout the lifetime of the process.
- Global variables are placed in the same region, since they must be accessible to everything in the process and likewise exist for the whole life of the process.
- The remaining space is used flexibly, not as a strict, hard division: dynamic allocation (malloc, calloc) takes space from the heap whenever the need arises later.
- The stack is placed in the process for function calls and recursion.
- The CPU takes the lines available in the executable code one by one and keeps executing them, referring during execution to the static variables of the same program — which is exactly why those variables are present inside the same process.
- The entire structure is the process boundary: the program may access only this part. Accessing any other part causes a segmentation fault, so the program is completely restricted to the elements present in its own part.
Layout summary (addresses increase upwards):
| Position (low to high address) | Contents | Property |
|---|---|---|
| Bottom | Executable code (.exe / a.out) | The instructions the CPU executes one by one |
| Above code | Static variables and global variables | Created once, exist for the entire lifetime of the process, referred to during execution |
| Above that (flexible region) | Heap | Dynamic allocation: malloc, calloc; used later whenever the need arises |
| Top | Stack (function stack) | Used for function calls and recursion |
Process Memory Layout
Drawing the diagram…
Shows the process memory regions ordered from low to high address: code, then static/global variables, then heap, then stack.
Worked examples
The layout is described in words, from the lowest address upwards:
- Lowest: executable code (.exe / a.out).
- Above the code: static variables and global variables.
- Above that, in the flexible region: the heap (malloc, calloc).
- Top: the stack (function stack).
Comparison
| Item | Static variables | Global variables |
|---|---|---|
| Creation | Created once | Created once |
| Lifetime | Remain forever, throughout the lifetime of the process | Remain forever, throughout the lifetime of the process |
| Access | Present in the same region of the process | Must be given access to everything in the process |
| Relation | — | A kind of static variable |
| Item | Heap | Stack |
|---|---|---|
| Purpose | Dynamic allocation (malloc, calloc) when the requirement is not known in advance | Function calling and recursion (function stack) |
| Nature | Space used later, at the time it is needed | Space used for calls |
Revision summary
- The executable code (.exe / a.out), static and global variables, heap and stack together form the process structure in main memory and define the process boundaries.
- Static and global variables are created once and live for the whole lifetime of the process — never created and destroyed repeatedly.
- The heap exists because data structures are not declared unless required; malloc and calloc take space from it when needed.
- The stack (function stack) exists for function calls and recursion.
- The CPU executes the executable code line by line and refers to the static variables kept in the same process.
- Accessing anything outside the process boundary causes a segmentation fault; the OS must never allow a process to cross its boundary.
- The remaining space in the process is not a strict division — it is shared flexibly between the growing needs of the process.
Previous-year exam insights
- Standard layout from low to high address: text (code), then initialized data, then uninitialized data (BSS), then heap (grows upward), then a free region, then stack (grows downward from the top of the address space).
- The heap grows toward higher addresses and the stack grows toward lower addresses; they grow toward each other, and a collision/overflow of either leads to a fault (stack overflow or heap exhaustion).
- Local (automatic) variables and function parameters live on the stack and are created/destroyed on each function call and return, unlike static/global variables which persist for the whole process lifetime.
- Static local variables (declared static inside a function) are allocated once in the data/BSS region, not on the stack, and retain their value between calls.
- The text (code) segment is typically read-only and shared between processes running the same program; writing to it causes a segmentation fault.
- A segmentation fault occurs when a process accesses an address outside its valid address space (e.g., null pointer, wild pointer, stack overflow), and the OS terminates it via a trap.