DOC PREVIEW
Berkeley COMPSCI 61C - C Memory Management

This preview shows page 1-2-15-16-17-32-33 out of 33 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

inst eecs berkeley edu cs61c CS61C Machine Structures Lecture 5 C Memory Management 2010 06 28 Instructor Paul Pearce Symmetric multiprocessor MIPS support for Android MIPS Technologies founded by John Hennessy author of your textbook recently announced support for SMP on Android This week you ll learn MIPS http tinyurl com 297ne2j CS61C L05 C Memory Management 1 Pearce Summer 2010 UCB Review Use handles to change pointers Create abstractions and your own data structures with structures Dynamically allocated heap memory must be manually deallocated in C Use malloc and free to allocate and de allocate persistent storage CS61C L05 C Memory Management 2 Pearce Summer 2010 UCB How big are structs Recall C operator sizeof which gives size in bytes of type or variable How big is sizeof p struct p char x int y 5 bytes 8 bytes Compiler may word align integer y Takeaway Structs can be padded use sizeof CS61C L05 C Memory Management 3 Pearce Summer 2010 UCB Don t forget the globals Remember Structure declaration does not allocate memory Variable declaration does allocate memory So far we have talked about several different ways to allocate memory for data 1 Declaration of a local variable int i struct Node list char string int ar n 3 Dynamic allocation at runtime by calling allocation function malloc ptr struct Node malloc sizeof struct Node n One more possibility exists 3 Data declared outside of any procedure i e before main Similar to 1 above but has global scope CS61C L05 C Memory Management 4 int myGlobal main Pearce Summer 2010 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 malloc storage data lives until deallocated by programmer C requires knowing where objects are in memory otherwise things don t work as expected Java hides location of objects CS61C L05 C Memory Management 5 Pearce Summer 2010 UCB Normal C Memory Management FFFF FFFFhex A program s address space contains 4 regions 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 code loaded when program starts does not change CS61C L05 C Memory Management 6 stack heap static data code hex For now OS somehow prevents accesses between stack and heap gray hash lines Wait for virtual memory Pearce Summer 2010 UCB Where are variables allocated If declared outside a procedure allocated in static storage If declared inside procedure allocated on the stack and freed when procedure returns main is a procedure int myGlobal main int myTemp CS61C L05 C Memory Management 7 Pearce Summer 2010 UCB The Stack Stack frame includes Return instruction address Parameters Space for other local variables Stack frames contiguous blocks of memory stack pointer tells where bottom stack frame is When procedure ends stack frame is tossed off the stack SP frees memory for future stack frames CS61C L05 C Memory Management 8 frame frame frame frame Pearce Summer 2010 UCB Stack Last In First Out LIFO data structure stack 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 CS61C L05 C Memory Management 9 Stack Stack Pointer grows down Stack Pointer Stack Pointer Stack Pointer Stack Pointer Pearce Summer 2010 UCB Who cares about stack management Pointers in C allow access to deallocated memory leading to hard to find bugs int fun main main main int y SP y 3 fun printf return y y 3 y SP SP main int stackAddr content stackAddr fun content stackAddr printf d content 3 content stackAddr printf d content 13451514 CS61C L05 C Memory Management 10 Pearce Summer 2010 UCB The Heap Dynamic memory Large pool of memory not allocated in contiguous order 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 CS61C L05 C Memory Management 11 Pearce Summer 2010 UCB Memory Management How do we manage memory Code Static storage are easy they never grow or shrink 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 CS61C L05 C Memory Management 12 Pearce Summer 2010 UCB Heap Management Requirements Want malloc and free to run quickly Want minimal memory overhead 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 This is technically called external fragmention CS61C L05 C Memory Management 13 Pearce Summer 2010 UCB Heap Management An example Request R1 for 100 bytes Request R2 for 1 byte Memory from R1 is R2 1 byte freed R1 100 bytes Request R3 for 50 bytes What if R3 was a request for 101 bytes CS61C L05 C Memory Management 14 Pearce Summer 2010 UCB Heap Management An example Request R1 for 100 bytes Request R2 for 1 byte Memory from R1 is R2 1 byte freed Request R3 for 50 bytes What if R3 was a request for 101 bytes CS61C L05 C Memory Management 15 R3 R3 Pearce Summer 2010 UCB Administrivia You made it through the first week Yay Don t forget about the newsgroup Hasn t been used as much as Paul would like Paul has elaborated on some things from lecture on the newsgroup We start assembly tomorrow Don t forget about the reading HW 2 due tomorrow at midnight Project 1 out now Start early lots of code to understand and it s really fun Check back of handout for CS Illustrated on pointers and arrays CS61C L05 C Memory Management 16 Pearce Summer 2010 UCB More Administrivia Tentative schedule for the rest of the summer now online Plan accordingly Midterm currently slated for Thursday July 15th from 6pm 9pm Does anyone have a hard conflict with this time Final time will be determined based on how the midterm goes at the scheduled time The final will be on Thursday August 12th Pick up a copy of the cheating policy from the front On it please write your name login and then sign it You ll need to give this to your Lab TA in order to be checked off for Lab 3 CS61C L05 C Memory Management


View Full Document

Berkeley COMPSCI 61C - C Memory Management

Documents in this Course
SIMD II

SIMD II

8 pages

Midterm

Midterm

7 pages

Lecture 7

Lecture 7

31 pages

Caches

Caches

7 pages

Lecture 9

Lecture 9

24 pages

Lecture 1

Lecture 1

28 pages

Lecture 2

Lecture 2

25 pages

VM II

VM II

4 pages

Midterm

Midterm

10 pages

Load more
Download C Memory Management
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view C Memory Management and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view C Memory Management 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?