










Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This chapter discusses the problem of not knowing the amount of variable space required at compile time and introduces dynamic memory as a solution. Dynamic memory is space allocated for variables at execution time using the operator 'new' in c++. The details of 'new', the heap, and how to access dynamic variables using pointers. Three examples are provided to illustrate dynamic memory allocation.
Typology: Study notes
1 / 18
This page cannot be seen from the preview
Don't miss anything!











Problem: What if we don’t know ahead of time (at compile time) the amount of variable space we will need? If we under estimate an array size, we’ll get a runtime error. If we over estimate an array size, we’ll have lots of unused memory. Solution: Allow variables to be created at execution time.
Dynamic Memory Defn: Dynamic memory is space that is allocated for variables at execution time. In C++, the operator new is used to create a variable dynamically. new does two things:
Example 1 : Dynamic Memory Allocation typedef int* int_ptr; int_ptr Count; Count = new int; If the heap looks like this:
Example 2 : Dynamic Memory Allocation typedef int* RecPointer; A. RecPointer P1, P2, P3;
C. P3 = P1;
D. P1 = new int;