{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

14-memory - Spring 2009 CS216 Program and Data...

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

View Full Document Right Arrow Icon
Memory Hierarchy & Locality CS216: Program and Data Representation University of Virginia Computer Science Spring 2009 Aaron Bloomfield
Image of page 1

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

View Full Document Right Arrow Icon
2 Outline • Concepts – Memory Allocation • Stack – static • Heap – Dynamic – Memory leak Memory Allocation in C – malloc() – alloca() – free() – realloc()
Image of page 2
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
Image of page 3

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

View Full Document Right Arrow Icon
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
Image of page 4
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 isn’t enough space.
Image of page 5

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

View Full Document Right Arrow Icon
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
Image of page 6
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 *
Image of page 7

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

View Full Document Right Arrow Icon
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
Image of page 8
9 End of lecture on Fri, April 17 • Also went over the last dozen slides of 13- graphs
Image of page 9

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

View Full Document Right Arrow Icon
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 wasn’t a recorded lecture
Image of page 10
11 Outline • Concepts – Memory Allocation • Stack – static • Heap – Dynamic – Memory leak • Memory Allocation in C – malloc() – alloca() – free() – realloc()
Image of page 11

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

View Full Document Right Arrow Icon
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
Image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern