Presentation is loading. Please wait.

Presentation is loading. Please wait.

Memory Layout C and Data Structures Baojian Hua

Similar presentations


Presentation on theme: "Memory Layout C and Data Structures Baojian Hua"— 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 binary files C source code process compiling running executable linking

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

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

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

7 Organization of Virtual Memory:.text Program code and constant binary form loaded libraries known as “ text ” segment space calculated at compile- time 0xffffffff 0 text

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; 0xffffffff 0 text data bss

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 0xffffffff 0 text data bss heap

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 0xffffffff 0 text data bss heap stack

11 Summary text: program text data: initialized globals & static data bss: un-initialized globals & static data heap: dynamically managed memory stack: function local variables 0xffffffff 0 text data bss heap stack

12 Example char *string = “hello”; int iSize; char *f (int x) { char *p; iSize = 8; p = (char *)malloc (iSize); return p; } 0xffffffff 0 text data bss heap stack

13 Example char *string = “hello”; int iSize; char *f (int x) { char *p; iSize = 8; p = (char *)malloc (iSize); return p; } 0xffffffff 0 text data bss heap stack

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 0xffffffff 0 text data bss heap stack

15 Example char *string = “hello”; int iSize; char *f (int x) { char* p; iSize = 8; p = (char *)malloc (iSize); return p; } 0xffffffff 0 text data bss heap stack program startup when f() is called live after allocation; till free() or program finish

16 Variable Initialization text: readonly data on program startup bss: un-initialized (though some systems initialize with 0) heap: un-initialized stack: un-initialized 0xffffffff 0 text data bss heap stack

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; } 0xffffffff 0 text data bss heap stack p

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

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

21 Aliasing int main() { int *p, *q; p = (int *)malloc (sizeof (*p)); *p = 99; q = p; return 0; } 0xffffffff 0 text data bss heap stack 99 p q

22 Aliasing int main() { int *p, *q; p = (int *)malloc (sizeof (*p)); *p = 99; q = p; *q = 88; return 0; } 0xffffffff 0 text data bss heap stack 88 p q

23 Aliasing int main() { int *p, *q; p = (int *)malloc (sizeof (*p)); *p = 99; q = p; *q = 88; free (q); return 0; } 0xffffffff 0 text data bss heap stack $%#^& p q

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; } 0xffffffff 0 text data bss heap stack $%#^& p q

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


Download ppt "Memory Layout C and Data Structures Baojian Hua"

Similar presentations


Ads by Google