{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lect10 - CMSC 216 Introduction to Computer Systems Lecture...

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

View Full Document Right Arrow Icon
CMSC 216 Introduction to Computer Systems Lecture 10 Dynamic Memory Allocation Jan Plane & Pete Keleher {jplane, keleher}@cs.umd.edu Administrivia Exam #1 Thursday – Review in lab tomorrow (Wednesday) – Practice Exam – Lecture Exercises – Lecture Outlines – Lab presentations, worksheets and exercises 2
Background image of page 1

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

View Full Document Right Arrow Icon
D YNAMIC M EMORY A LLOCATION Chapter 11&12, Reek 3 Stack vs. Heap Memory Stack memory is allocated and deallocated in a specific order – you can only remove from and add to the top of the stack – any item in the stack is always freed before the item below it – useful for local variables, since functions work in similar manner Heap memory – can be allocated and deallocated in any order while the program is running 4
Background image of page 2
Dynamically allocated memory Why use it? – to eliminate compile-time limits: Often, the size of a data structure isn't known until runtime Allocating a huge amount of space to fit every possible scenario can be error-prone at worst and a waste of space at best User-managed heap vs. Garbage collection – garbage collection: frees programmer from burden of keeping track of objects (convenience) slows down execution due to collection needing to be run periodically; collection is also expensive – user-managed heap programmer can control the precise time to deallocate objects requires a lot more care - errors can occur if the wrong thing is freed, or the right thing is freed at the wrong time 5 Memory management functions Allocating memory: void *malloc(size_t amount); • allocates amount bytes of memory (if available) from the heap and returns a pointer to the beginning of it no initialization of the space void *calloc(size_t count, size_t obj_size); • allocates count objects of size obj_size each (if memory is available), returns a pointer to beginning of it
Background image of page 3

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

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}