inst eecs berkeley edu cs61c CS61C Machine Structures Lecture 6 C Memory Management 2004 09 13 www cs berkeley edu ddgarcia Cal ranked in the top 10 After a convincing win over Air Force we re now ranked 10 in all polls USA ESPN AP We face Southern Miss 1 0 on Thursday Barry Bonds also homered to reach 699 HRs Garcia Fall 2004 UCB Where allocated CS 61C L06 C Memory Management 2 Garcia Fall 2004 UCB Stack frame includes Return address Parameters Variable declaration does allocate memory Space for other local variables If declare outside a procedure allocated in static storage If declare inside procedure allocated on the stack and freed when int myGlobal procedure returns main NB main is a int myTemp procedure CS 61C L06 C Memory Management 3 Garcia Fall 2004 UCB Stack SP Stack frames contiguous blocks of memory stack pointer tells where top stack frame is When procedure ends stack frame is tossed off the stack frees memory for future stack frames CS 61C L06 C Memory Management 4 frame frame frame frame Garcia Fall 2004 UCB Who cares about stack management Pointers in C allow access to deallocated memory leading to hard to find bugs Last In First Out LIFO memory stack usage CS 61C L06 C Memory Management 5 struct point int x int y The Stack Structure declaration does not allocate memory main a 0 void a int m b 1 void b int n c 2 void c int o d 3 void d int p struct point int x int y should have been written Lecturer PSOE Dan Garcia CS 61C L06 C Memory Management 1 Clarifications to Friday s lecture int ptr main main main int y SP y 3 ptr printf return y y 3 y SP SP main int stackAddr content stackAddr ptr content stackAddr printf d content 3 content stackAddr printf d content 13451514 Stack Pointer Stack Pointer Stack Pointer Stack Pointer Stack Pointer Garcia Fall 2004 UCB CS 61C L06 C Memory Management 6 Garcia Fall 2004 UCB C Memory Management C has 3 pools of memory Static storage global variable storage basically permanent entire program run The Stack local variable storage parameters return address location of activation records in Java or stack frame in C The Heap dynamic storage data lives until deallocated by programmer C requires knowing where objects are in memory otherwise don t work as expect Java hides location of objects CS 61C L06 C Memory Management 7 Garcia Fall 2004 UCB Review Normal C Memory Management FFFF FFFFhex A program s address space contains 4 regions back to back requests for heap memory could result blocks very far apart where Java new command allocates memory In C specify number of bytes of memory explicitly to allocate item int ptr ptr int malloc sizeof int malloc returns type void so need to cast to right type malloc Allocates raw uninitialized memory from heap CS 61C L06 C Memory Management 8 Garcia Fall 2004 UCB Intel 80x86 C Memory Management A C program s 80x86 address space stack stack local variables grows downward heap space requested for pointers via malloc resizes dynamically grows upward static data variables declared outside main does not grow or shrink 0 The Heap Dynamic memory Large pool of memory not allocated in contiguous order heap space requested for pointers via malloc resizes dynamically grows upward heap static data code hex For now OS somehow code loaded when prevents accesses between program starts does not stack and heap gray hash change lines Wait for virtual memory CS 61C L06 C Memory Management 9 Garcia Fall 2004 UCB static data variables declared outside main does not grow or shrink 08000000 code loaded when program starts does not change stack local variables grows downward heap static data hex code stack CS 61C L06 C Memory Management 10 Garcia Fall 2004 UCB Memory Management Heap Management Requirements How do we manage memory Want malloc and free to run quickly Code Static storage are easy they never grow or shrink Want minimal memory overhead Stack space is also easy stack frames are created and destroyed in last in first out LIFO order Managing the heap is tricky memory can be allocated deallocated at any time CS 61C L06 C Memory Management 11 Garcia Fall 2004 UCB Want to avoid fragmentation when most of our free memory is in many small chunks In this case we might have many free bytes but not be able to satisfy a large request since the free bytes are not contiguous in memory CS 61C L06 C Memory Management 12 Garcia Fall 2004 UCB Heap Management Heap Management An example Request R1 for 100 bytes An example R1 100 bytes Request R1 for 100 bytes Request R2 for 1 byte R2 1 byte Memory from R1 is freed Request R2 for 1 byte R2 1 byte Memory from R1 is freed Request R3 for 50 bytes Request R3 for 50 bytes CS 61C L06 C Memory Management 13 Garcia Fall 2004 UCB CS 61C L06 C Memory Management 14 R3 R3 Garcia Fall 2004 UCB K R Malloc Free Implementation K R Implementation From Section 8 7 of K R malloc searches the free list for a block that is big enough If none is found more memory is requested from the operating system Code in the book uses some C language features we haven t discussed and is written in a very terse style don t worry if you can t decipher the code Each block of memory is preceded by a header that has two fields size of the block and a pointer to the next block All free blocks are kept in a linked list the pointer field is unused in an allocated block CS 61C L06 C Memory Management 15 Garcia Fall 2004 UCB free checks if the blocks adjacent to the freed block are also free If so adjacent free blocks are merged coalesced into a single larger free block Otherwise the freed block is just added to the free list CS 61C L06 C Memory Management 16 Garcia Fall 2004 UCB Choosing a block in malloc Administrivia Posting to the Newsgroup If there are multiple free blocks of memory that are big enough for some request how do we choose which one to use What IS allowed best fit choose the smallest block that is big enough for the request first fit choose the first block we see that is big enough next fit like first fit but remember where we finished searching and resume searching from there CS 61C L06 C Memory Management 17 Garcia Fall 2004 UCB Clarifying questions to make sure you understand the spec correctly What is NOT allowed Explaining your design Providing test cases Posting code of any kind be it Working code to provide help Buggy code to request help Anything that would deprive another student from the good think tackling a tough problem from start through finish CS 61C L06 C Memory Management 18 Garcia Fall
View Full Document
Unlocking...