310 likes | 619 Views
Memory Layout. C and Data Structures Baojian Hua bjhua@ustc.edu.cn. Goals of Today ’ s Lecture. Behind the scenes of running a program Code, executable, and process Memory layout for Linux processes, and relationship to C Explicit memory management in C void *malloc (int bytes);
E N D
Memory Layout C and Data Structures Baojian Hua bjhua@ustc.edu.cn
Goals of Today’s Lecture • Behind the scenes of running a program • Code, executable, and process • Memory layout for Linux processes, and relationship to C • Explicit memory management in C • void *malloc (int bytes); • allocate memory from the heap • free: deallocate memory from the heap
From C Code to Process • C source files • .c; .h • Binary files • .o • Executables • a.out • Process • Managed by OS C source code compiling binary files linking executable running process
Main Memory Network Audio Data Bus CPU Disk Video Memory shared by all processes
Virtual Memory • Continuous memory space for all process • each with its physical space • pretends you the same virtual space 0 0xffffffff
Organization of Virtual Memory: .text • Program code and constant • binary form • loaded libraries 0 text 0xffffffff
Organization of Virtual Memory: .text • Program code and constant • binary form • loaded libraries • known as “text” segment • space calculated at compile-time 0 text 0xffffffff
Organization of Virtual Memory: .data • Data: initialized global data in the program • Ex: int size = 100; • BSS: un-initialized global data in the program • Ex: int length; 0 text data bss 0xffffffff
Organization of Virtual Memory: heap • Heap: dynamically-allocated spaces • Ex: malloc, free • OS knows nothing about it • space • content • dynamically grows as program runs 0 text data bss heap 0xffffffff
Organization of Virtual Memory: stack • Stack: local variables in functions • we’ll discuss stack soon • support function call/return and recursive functions • grow to low address 0 text data bss heap stack 0xffffffff
Summary • text: program text • data: initialized globals & static data • bss: un-initialized globals & static data • heap: dynamically managed memory • stack: function local variables 0 text data bss heap stack 0xffffffff
Example char *string = “hello”; int iSize; char *f (int x) { char *p; iSize = 8; p = (char *)malloc (iSize); return p; } 0 text data bss heap stack 0xffffffff
Example char *string =“hello”; int iSize; char *f (int x) { char *p; iSize = 8; p = (char *)malloc (iSize); return p; } 0 text data bss heap stack 0xffffffff
Variable Lifetime • text: • program startup • program finish • data, bss: • program startup • program finish • heap: • dynamically allocated • de-allocated (free) • stack: • function call • function return 0 text data bss heap stack 0xffffffff
program startup when f() is called live after allocation; till free() or program finish Example char *string = “hello”; int iSize; char *f (int x) { char* p; iSize = 8; p = (char *)malloc (iSize); return p; } 0 text data bss heap stack 0xffffffff
Variable Initialization • text: • readonly • data • on program startup • bss: • un-initialized (though some systems initialize with 0) • heap: • un-initialized • stack: • un-initialized 0 text data bss heap stack 0xffffffff
Explicit Memory Management • Heap management in C is explicit • void *malloc (int bytes); • free (void *p); • It’s the programmers’ responsibility to make sure that such a sequence of action is safe
Example int main() { int *p; p = (int *)malloc (sizeof (*p)); *p = 99; return 0; } 0 text data bss heap p stack 0xffffffff
Example int main() { int *p; p = (int *)malloc (sizeof (*p)); *p = 99; return 0; } 0 text data bss heap #@%*& p stack 0xffffffff
Example int main() { int *p; p = (int *)malloc (sizeof (*p)); *p = 99; return 0; } 0 text data bss heap 99 p stack 0xffffffff
Aliasing int main() { int *p, *q; p = (int *)malloc (sizeof (*p)); *p = 99; q = p; return 0; } 0 text data bss heap 99 q p stack 0xffffffff
Aliasing int main() { int *p, *q; p = (int *)malloc (sizeof (*p)); *p = 99; q = p; *q = 88; return 0; } 0 text data bss heap 88 q p stack 0xffffffff
Aliasing int main() { int *p, *q; p = (int *)malloc (sizeof (*p)); *p = 99; q = p; *q = 88; free (q); return 0; } 0 text data bss heap $%#^& q p stack 0xffffffff
Dangling Reference int main() { int *p, *q; p = (int *)malloc (sizeof (*p)); *p = 99; q = p; *q = 88; free (q); *p = 77; return 0; } 0 text data bss heap $%#^& q p stack 0xffffffff
Memory Leaking int main() { int *p; p = (int *)malloc (sizeof (*p)); // make the above space unreachable p = (int *)malloc (sizeof (*p)); // even worse… while (1) p = (int *)malloc (sizeof (*p)); return 0; }
Memory Leaking void f (); void f () { int *p; p = (int *)malloc (sizeof (*p)); return; } int main () { f (); return 0; }
Summary • Dangling pointers and memory leaking 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