CSE361S-MemoryAllocation - Page 1 Harsh Reality Harsh...

Info iconThis preview shows pages 1–2. 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
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Page 1 Harsh Reality 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 z 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 2 Dynamic Memory Allocation Dynamic Memory Allocation Explicit vs. Implicit Memory Allocator Explicit vs. Implicit Memory Allocator Explicit: application allocates and frees space z E.g., malloc and free in C Implicit: application allocates, but does not free space z 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 3 Process Memory Image 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. 4 Malloc Package Malloc Package #include <stdlib.h> #include <stdlib.h> void *malloc(size_t size) void *malloc(size_t size) If successful: z Returns a pointer to a memory block of at least size bytes, (typically) aligned to 8-byte boundary. z 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. 5 Malloc Example 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 */ } 6 Assumptions Assumptions Assumptions made in this lecture Assumptions made in this lecture Memory is word addressed (each word can hold a pointer)...
View Full Document

This document was uploaded on 11/13/2009.

Page1 / 5

CSE361S-MemoryAllocation - Page 1 Harsh Reality Harsh...

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

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