class21 - Dynamic Memory Allocation I Nov 5 2002 Topics...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Dynamic Memory Allocation I Nov 5, 2002 Topics Topics Simple explicit allocators Data structures Mechanisms Policies class21.ppt 15-213 “The course that gives CMU its Zip!” – 2 – 15-213, F’02 Harsh Reality Memory Matters Memory Matters Memory is not unbounded Memory is not unbounded It must be allocated and managed Many applications are memory dominated Especially those based on complex, graph algorithms Memory referencing bugs especially pernicious Memory referencing bugs especially pernicious Effects are distant in both time and space Memory performance is not uniform Memory performance is not uniform Cache and virtual memory effects can greatly affect program performance Adapting program to characteristics of memory system can lead to major speed improvements – 3 – 15-213, F’02 Dynamic Memory Allocation Explicit vs. Implicit Memory Allocator Explicit vs. Implicit Memory Allocator 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 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 simple explicit memory allocation today Will discuss simple explicit memory allocation today Application Dynamic Memory Allocator Heap Memory – 4 – 15-213, F’02 Process Memory Image kernel virtual memory Memory mapped region for shared libraries run-time heap (via malloc ) program text (. text ) initialized data (. data ) uninitialized data (. bss ) stack %esp memory invisible to user code the “ brk ” ptr Allocators request additional heap memory from the operating system using the sbrk function. – 5 – 15-213, F’02 Malloc Package #include <stdlib.h> #include <stdlib.h> void *malloc(size_t size) 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. If size == 0 , returns NULL If unsuccessful: returns NULL (0) and sets errno . void free(void *p) 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) 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. – 6 – 15-213, F’02 Malloc Example void foo(int n, int m) { int i, *p; /* allocate a block of n ints */ if ((p = (int *) malloc(n * sizeof(int))) == NULL) { perror("malloc"); exit(0); } for (i=0; i<n; i++) p[i] = i; /* add m bytes to end of p block */ if ((p = (int *) realloc(p, (n+m) * sizeof(int))) == NULL) { perror("realloc"); exit(0); } for (i=n; i < n+m; i++) p[i] = i; /* print new array */ for (i=0; i<n+m; i++) printf("%d\n", p[i]); free(p); /* return p to available memory pool */ } – 7 –...
View Full Document

This note was uploaded on 11/17/2009 for the course CS:APP 422 taught by Professor Randale.bryantanddavidr.o'hallaron during the Spring '02 term at Carnegie Mellon.

Page1 / 30

class21 - Dynamic Memory Allocation I Nov 5 2002 Topics...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online