Lecture15-memory-allocation - Announcements Project 3 due...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 CS 3214 Computer Systems Godmar Back Lecture 15 Announcements Project 3 due Oct 14 Exercise 5 due Oct 18 – Extended by 1 day – see piazza Midterm Oct 25 – See announcement See sample midterms/final on website CS 3214 Fall 2011 2 10/17/2011 Some of the following slides are taken with permission from Complete Powerpoint Lecture Notes for Computer Systems: A Programmer's Perspective (CS:APP) Randal E. Bryant and David R. O'Hallaron http://csapp.cs.cmu.edu/public/lectures.html MEMORY MANAGEMENT Part 1 10/17/2011 CS 3214 Fall 2011 3 Dynamic Memory Allocation Explicit vs. Implicit Memory Allocator Application Dynamic Memory Allocator Heap Memory – Explicit: application allocates and frees space E.g., malloc and free in C Implicit: application allocates, but does not free space E.g. garbage collection in Java, ML or Lisp Allocation In both cases the memory allocator provides an abstraction of memory as a set of blocks Doles out free memory blocks to application Will discuss explicit memory allocation today 10/17/2011 4 CS 3214 Fall 2011 Process Memory Image kernel virtual memory Memory mapped region for shared libraries stack %esp memory invisible to user code Allocators request additional heap memory from the operating system using the sbrk function. run-time heap (via malloc ) program text ( . text ) initialized data ( . data ) uninitialized data ( . bss ) 0 the “ brk ” ptr Initial start of the heap is randomized (a bit above end of .bss, usually) 10/17/2011 5 CS 3214 Fall 2011 The Malloc API #include <stdlib.h> void *malloc(size_t size) If successful: Returns a pointer to a memory block of at least size bytes, (typically) aligned to 8-byte boundary; use memalign() for other alignments If size == 0 , may return either NULL or a pointer that must be freed (platform-dependent) – If unsuccessful: returns NULL (0) and sets errno . If unsuccessful: returns NULL (0) and sets void free(void *p) Returns the block pointed at by p to pool of available memory – p must come from a previous call to malloc or realloc . void *realloc(void *p, size_t size) Changes size of block p and returns pointer to new block. Contents of new block unchanged up to min of old and new size. 10/17/2011 6 CS 3214 Fall 2011
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon