1 / 27

Memory Layout

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);

loren
Download Presentation

Memory Layout

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Memory Layout C and Data Structures Baojian Hua bjhua@ustc.edu.cn

  2. 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

  3. 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

  4. Main Memory Network Audio Data Bus CPU Disk Video Memory shared by all processes

  5. Virtual Memory • Continuous memory space for all process • each with its physical space • pretends you the same virtual space 0 0xffffffff

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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. Example int main() { int *p; p = (int *)malloc (sizeof (*p)); *p = 99; return 0; } 0 text data bss heap p stack 0xffffffff

  19. Example int main() { int *p; p = (int *)malloc (sizeof (*p)); *p = 99; return 0; } 0 text data bss heap #@%*& p stack 0xffffffff

  20. Example int main() { int *p; p = (int *)malloc (sizeof (*p)); *p = 99; return 0; } 0 text data bss heap 99 p stack 0xffffffff

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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; }

  26. Memory Leaking void f (); void f () { int *p; p = (int *)malloc (sizeof (*p)); return; } int main () { f (); return 0; }

  27. 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

More Related