Roadmap
C Memory Layout
Program’s address space
contains 4 regions:
Stack: local variables, grows downward
Heap: space requested via malloc() and used with pointers; resizes dynamically, grows upward
Static Data: global and static variables, does not grow or shrink
Code: loaded when program
starts, does not change
1
code
static data
heap
stack
~ FFFF FFFFhex
~ 0hex
OS prevents accesses
between stack and heap
(via virtual memory)
CMPT 295
Week 2 – Summary
End-to-End Example
2
void foo(int n, int m) {
int i, *p;
p = (int*) malloc(n*sizeof(int)); /* allocate block of n ints */
if (p == NULL) { /* check for allocation error */
perror(“malloc”);
exit(0);
}
for (i=0; i