14-memory

14-memory - Memory Hierarchy & Locality CS216: Program...

Info iconThis preview shows pages 1–13. 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

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: Memory Hierarchy & Locality CS216: Program and Data Representation University of Virginia Computer Science Spring 2009 Aaron Bloomfield 2 Outline Concepts Memory Allocation Stack static Heap Dynamic Memory leak Memory Allocation in C malloc() alloca() free() 2 Outline Concepts Memory Allocation Stack static Heap Dynamic Memory leak Memory Allocation in C malloc() alloca() free() realloc() 3 Memory in C Stack Managed by compiler automatically Lifetime is determined by program scope Cannot outlive procedure return Address space grows down from the top Heap Managed by programmer explicitly Lifetime is controlled by programmer Lives until freed by program Address space grows up from the bottom 4 Static/Dynamic Allocation Static : space required is known before program starts (at compile time) Dynamic : space required is not known before the program starts Can be placed on either the stack or the heap But most often on the heap 5 malloc void *malloc (size_t size ) Returns an untyped pointer (can point to anything) Size in bytes Returns an address that is the location of at least size bytes of previously unused memory, and reserves that space. Returns null is there isnt enough space. 6 Memory Allocators Lifetime Scoped Unlimited Known Unknown malloc alloca like malloc, but on the stack , not heap (rarely used) Size alloca local variable declarations global, static variable declarations 7 malloc Example char *s = (char *) malloc (sizeof(*s) * n) sizeof operator Takes a type or expression , evaluates to the number of bytes to store it type cast malloc returns void * cast tells compiler that program will use it as a char * 8 string functions int strlen(char * s ) returns number of chars in s char *strcpy(char * s1 , const char * s2 ) Copies s2 to s1 char *strcat(char * s1 , const char * s2 )- Appends s2 to s1 9 End of lecture on Fri, April 17 Also went over the last dozen slides of 13- graphs 10 End of lecture on Mon, April 20 Today was a guest lecture by Westley Weimer He went over aspect-oriented programming, which can be found in the 15-aop.pptx slide set It wasnt a recorded lecture 11 Outline Concepts Memory Allocation Stack static Heap Dynamic Memory leak Memory Allocation in C malloc() alloca() free() realloc() 12 Memory in C Stack Managed by compiler automatically Lifetime is determined by program scope Cannot outlive procedure return Address space grows down from the top Heap Managed by programmer explicitly Lifetime is controlled by programmer Lives until freed by program Address space grows up from the bottom 13...
View Full Document

This note was uploaded on 09/11/2009 for the course CS 216 taught by Professor Bloomfield during the Spring '08 term at UVA.

Page1 / 49

14-memory - Memory Hierarchy & Locality CS216: Program...

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

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