Академический Документы
Профессиональный Документы
Культура Документы
Executables linking
a.out executable
Process running
Managed by OS process
Main Memory
Network Audio
Data Bus
CPU
Disk Video
Memory
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
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;
}
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
q = p; // alias 99
return 0;
} q
p
0xffffffff stack
Example
int main()
{ 0
int *p; text
0xffffffff stack
Example
int main()
{ 0
int *p; text
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