内存管理

本章节介绍了C语言的内存管理,执行文件在存储时可以分为代码区、数据区以及未初始数据区三个部分,本章节关于这三个部分做了介绍,另外本章节关于堆区也做出了介绍,堆区是bug的肥沃土壤,在此我们应该注意的地方。
展开查看详情

1.CS 61C: Great Ideas in Computer Architecture Lecture 4: Memory Management Krste Asanović & Randy Katz http://inst.eecs.berkeley.edu/~cs61c

2.Agenda Pointers to Pointers Strings in C C Memory Management Stack Heap Implementations of malloc /free Common memory problems & how to avoid/find them And in Conclusion, … CS 61c Lecture 4: Memory Management 2

3.Pointers to Pointers CS 61c 3

4.Your Turn … int x [] = { 2, 4, 6, 8, 10 }; int *p = x; int **pp = &p ; (*pp)++; (*(*pp))++ ; printf ("%d

5.Agenda Pointers to Pointers Strings in C C Memory Management Stack Heap Implementations of malloc /free Common memory problems & how to avoid/find them And in Conclusion, … CS 61c 5

6.C Strings C strings are null-terminated character arrays char s[] = ”abc”; CS 61c Lecture 3: Pointers 6 Type Name Byte Addr Value … 108 107 106 105 104 103 102 101 100 …

7.String Example CS 61c 7 Output: str = abc , length = 3

8.Concise strlen () int strlen (char *s) { char *p = s; while (*p++) ; /* Null body of while */ return (p – s – 1); } What happens if there is no zero character at end of string? 8 CS 61c

9.Arguments in main() To get arguments to the main function, use: int main( int argc , char * argv []) argc is the number of strings on the command line argv is a pointer to an array containing the arguments as strings 9 CS 61c

10.Example 10 CS 61c UNIX: $ gcc -o ex Argc.c $ ./ex -g a "d e f” arg [0 ] = ./ ex arg [1 ] = - g arg [2 ] = a arg [3 ] = d e f

11.Agenda Pointers Wrap-up Strings in C C Memory Management Stack Heap Implementations of malloc /free Common memory problems & how to avoid/find them And in Conclusion, … CS 61c Lecture 4: Memory Management 11

12.C Memory Management How does the C compiler determine where to put code and data in the machine’s memory? How can we create dynamically sized objects? E.g. array of variable size depending on requirements CS 61c Lecture 4: Memory Management 12 currently unused but available memory code static data heap stack ~ FFFF FFFF hex ~ 0000 0000 hex Sample Layout (32-bit addresses)

13.C Memory Management: Code CS 61c 13 Code Loaded when program starts Does not change currently unused but available memory code static data heap stack ~ FFFF FFFF hex ~ 0000 0000 hex Sample Layout (32-bit addresses)

14.C Memory Management: Static Data CS 61c 14 Static Data Loaded when program starts Can be modified Size is fixed currently unused but available memory code static data heap stack ~ FFFF FFFF hex ~ 0000 0000 hex Sample Layout (32-bit addresses)

15.C Memory Management: Stack CS 61c 15 Stack Local variables & arguments inside functions Allocated when function is called Stack usually grows downward currently unused but available memory code static data heap stack ~ FFFF FFFF hex ~ 0000 0000 hex Sample Layout (32-bit addresses)

16.C Memory Management: Heap CS 61c 16 Heap Space for dynamic data Allocated and freed by program as needed currently unused but available memory code static data heap stack ~ FFFF FFFF hex ~ 0000 0000 hex Sample Layout (32-bit addresses)

17.Agenda Pointers Wrap-up Strings in C C Memory Management Stack Heap Implementations of malloc /free Common memory problems & how to avoid/find them And in Conclusion, … CS 61c Lecture 4: Memory Management 17

18.Frame main Frame b int arg ; int b_local ; Frame b int arg ; int b_local ; Frame a int a_local ; Stack CS 61c Lecture 4: Memory Management 18 Stack Pointer Stack Pointer Stack Pointer Stack Pointer

19.Stack Every time a function is called, a new frame is allocated When the function returns, the frame is deallocated Stack frame contains Function arguments Local variables Return address (who called me?) Stack uses contiguous blocks of memory Stack pointer indicates current level of stack Stack management is transparent to C programmer We’ll see details when we program assembly language CS 61c Lecture 4: Memory Management 19

20.Your Turn … Right after the printf executes but before the return 0 , how many copies of x and y are allocated in memory? CS 61c Lecture 4: Memory Management 20 Answer #x #y RED 1 1 GREEN 1 6 ORANGE 1 5 YELLOW 6 6

21.What’s wrong with this Code? *a is a pointer to a local variable allocated on the stack “deallocated” when f() returns stack reused by other functions e.g. cos which overwrite whatever was there before *a points to “garbage” Obscure errors depend on what other functions are called after f() returns assignments to *a corrupt the stack and can result in even more bizarre behavior than this example errors can be difficult to reproduce CS 61c Lecture 4: Memory Management 21 Output : a = -1085663214

22.Agenda Pointers Wrap-up Strings in C C Memory Management Stack Not on the test! Heap Implementations of malloc /free Common memory problems & how to avoid/find them And in Conclusion, … CS 61c Lecture 4: Memory Management 22

23.Early Memory Technology CS 61c Lecture 4: Memory Management 23 Punched cards, From early 1700s through Jaquard Loom, Babbage, and then IBM Babbage, 1800s: Digits stored on mechanical wheels

24.Early Memory technology CS 61c Lecture 4: Memory Management 24 Williams Tube, Manchester Mark 1, 1947 Mercury Delay Line, Univac 1, 1951 Punched paper tape, instruction stream in Harvard Mk 1

25.MIT Whirlwind Core Memory 25

26.Modern DRAM Technology CS 61c Lecture 4: Memory Management 26 46nm DDR2 DRAM [© Chipworks ]

27.Break! 9/4/17 27 Fall 2017 - Lecture #3

28.Agenda Pointers Wrap-up Strings in C C Memory Management Stack Heap Implementations of malloc /free Common memory problems & how to avoid/find them And in Conclusion, … CS 61c Lecture 4: Memory Management 28

29.Managing the Heap C functions for heap management: malloc() allocate a block of uninitialized memory calloc () allocate a block of zeroed memory free() free previously allocated block of memory realloc () change size of previously allocated block Beware : previously allocated contents might move! CS 61c Lecture 4: Memory Management 29