You are on page 1of 30

Memory Layout

C and Data Structures


Baojian Hua
bjhua@ustc.edu.cn
Goals of Todays Lecture
Behind the scenes of running a program
Code, executable, and process
Memory layout for UNIX processes, and
relationship to C
Explicit memory management in C
malloc: allocate memory from the heap
free: deallocate memory from the heap
From C Code to Process
C source files
C source code
.c; .h
compiling
Binary files
.o binary files

Executables linking
a.out executable
Process running
Managed by OS process
Main Memory

Network Audio

Data Bus
CPU

Disk Video
Memory

shared by all processes


Virtual Memory
Continuous memory space 0

for all process


each with its physical space
pretends you the same
virtual space

0xffffffff
Organization of Virtual
Memory: .text
Program code and 0
text
constant
binary form
loaded libraries

0xffffffff
Organization of Virtual
Memory: .text
Program code and 0
text
constant
binary form
loaded libraries
known as text segment
space calculated at compile-
time
0xffffffff
Organization of Virtual
Memory: .data
Data: initialized global data 0
text
in the program
data
Ex: int size = 100; bss
BSS: un-initialized global
data in the program
Ex: int length;

0xffffffff
Organization of Virtual
Memory: heap
Heap: dynamically- 0
text
allocated spaces
data
Ex: malloc, free bss
OS knows nothing about it
heap
space
content
dynamically grows as
program runs 0xffffffff
Organization of Virtual
Memory: stack
Stack: local variables in 0
text
functions
data
well discuss stack soon bss
support function call/return
heap
and recursive functions
grow to low address

0xffffffff stack
Summary
text: program text 0
text
data: initialized globals & static
data
data
bss
bss: un-initialized globals &
static data heap

heap: dynamically managed


memory
stack: function local variables stack
0xffffffff
Example
char *string = hello;
0
int iSize; text

data
char *f(int x)
{ bss
char *p;
heap
iSize = 8;
p = malloc (iSize);
return p;
}

0xffffffff stack
Example
char *string = hello;
0
int iSize; text

data
char *f (int x)
{ bss
char *p;
heap
iSize = 8;
p = malloc (iSize);
return p;
}

0xffffffff stack
Variable Lifetime
text:
0
program startup text
program finish
data, bss: data
program startup bss
program finish
heap: heap
dynamically allocated
de-allocated (free)
stack:
function call
function return 0xffffffff stack
Example
char *string = hello;
program 0
int iSize; startup text

data
char *f (int x)
{ bss
char* p; when f()
is called heap
iSize = 8;
p = malloc (iSize);
return p;
}

live after allocation; 0xffffffff stack


till free() or program
finish
Variable Initialization
text:
0
readonly text
data data
on program startup
bss
bss:
un-initialized (though some systems heap
initialize with 0)
heap:
un-initialized
stack:
un-initialized 0xffffffff stack
Explicit Memory Management
Heap management in C is explicit
malloc to request a region
free to recycle a region
Its the programmers responsibility to
make sure that such a sequence of
action is safe
Example
int main()
{ 0
int *p; text

data
p = malloc (4);
*p = 99; bss

return 0; heap
}

0xffffffff stack
Example
int main()
{ 0
int *p; text

data
p = malloc (4);
*p = 99; bss

return 0; heap
}
#@%*&

0xffffffff stack
Example
int main()
{ 0
int *p; text

data
p = malloc (4);
*p = 99; bss

return 0; heap
}
99

0xffffffff stack
Example
int main()
{ 0
int *p; text

data
p = malloc (4);
*p = 99; bss

int *q; heap

q = p; // alias 99

return 0;
} q
p

0xffffffff stack
Example
int main()
{ 0
int *p; text

p = malloc (4); data


*p = 99;
bss
int *q;
heap
q = p;
99
printf (%d\n, *q);
return 0; q
}
p

0xffffffff stack
Example
int main()
{ 0
int *p; text

p = malloc (4); data


*p = 99;
bss
int *q;
heap
q = p;
printf (%d\n, *q);
free (q); q
return 0; p
}
0xffffffff stack
Dangling Pointer Reference
int main()
{
int *p, *q;

q = p = (int *) malloc (4);


free (q);
*p;

return 0;
}
Dangling Dereference
int main()
{ 0
int *p, *q; text

data
q = p = (int *) malloc (4);
free (q); bss
*p;
heap
return 0;
} #@%*&

p
q
0xffffffff stack
Dangling Dereference
int main()
{ 0
int *p, *q; text

data
q = p = (int *) malloc (4);
free (q); bss
*p;
heap
return 0;
}

p
q
0xffffffff stack
Dangling Dereference
int main()
{ 0
int *p, *q; text

data
q = p = (int *) malloc (4);
free (q); bss
*p; // no this memory!!!
heap
return 0;
}

p
q
0xffffffff stack
Memory Leak
int main()
{
int *p;
p = (int *) malloc (4);
// make the above space unreachable
p = (int *) malloc (4);
// even worse
while (1)
p = malloc (4);
return 0;
}
Memory Leak
void f ();

void f ()
{
int *p;
p = malloc (4);
return;
}

int main ()
{
f ();
return 0;
}
Summary
Dangling pointers and memory leak are
evil sources of bugs:
hard to debug
may fire after a long time of run
may far from the bug point
hard to prevent
especially by using the static methods
Part of the reasons for the popularity of
garbage collection

You might also like