Dynamic Memory A whole heap of fun…
Review: The Stack
• C++ allocates variables on a stackvoid foo(int q) {
if(true) { char c = 'a'; }}
int main() { int x = 10; double y = 1.2; foo(5); int z = 5;}
Address Identifier Value116115114113112111110109108107106105104103102101100
The Stack
• C++ allocates variables on a stackvoid foo(int q) {
if(true) { char c = 'a'; }}
int main() { int x = 10; double y = 1.2; foo(5); int z = 5;}
Address Identifier Value116115114113112111110109108107106105104103
X 10102101100
The Stack
• C++ allocates variables on a stackvoid foo(int q) {
if(true) { char c = 'a'; }}
int main() { int x = 10; double y = 1.2; foo(5); int z = 5;}
Address Identifier Value116115114113112111
Y 1.2
110109108107106105104103
X 10102101100
The Stack
• C++ allocates variables on a stackvoid foo(int q) {
if(true) { char c = 'a'; }}
int main() { int x = 10; double y = 1.2; foo(5); int z = 5;}
Address Identifier Value116115
q 5114113112111
Y 1.2
110109108107106105104103
X 10102101100
The Stack
• C++ allocates variables on a stackvoid foo(int q) {
if(true) { char c = 'a'; }}
int main() { int x = 10; double y = 1.2; foo(5); int z = 5;}
Address Identifier Value116 c a115
q 5114113112111
Y 1.2
110109108107106105104103
X 10102101100
The Stack
• C++ allocates variables on a stackvoid foo(int q) {
if(true) { char c = 'a'; }}
int main() { int x = 10; double y = 1.2; foo(5); int z = 5;}
Address Identifier Value116115
q 5114113112111
Y 1.2
110109108107106105104103
X 10102101100
The Stack
• C++ allocates variables on a stackvoid foo(int q) {
if(true) { char c = 'a'; }}
int main() { int x = 10; double y = 1.2; foo(5); int z = 5;}
Address Identifier Value116115114113112111
Y 1.2
110109108107106105104103
X 10102101100
The Stack
• C++ allocates variables on a stackvoid foo(int q) {
if(true) { char c = 'a'; }}
int main() { int x = 10; double y = 1.2; foo(5); int z = 5;}
Address Identifier Value116115
z 5114113112111
Y 1.2
110109108107106105104103
X 10102101100
The Stack
• C++ allocates variables on a stackvoid foo(int q) {
if(true) { char c = 'a'; }}
int main() { int x = 10; double y = 1.2; foo(5); int z = 5;}
Address Identifier Value116115114113112111110109108107106105104103102101100
What will this do?
• getPointerToTen – Initialize a variable– Makes a pointer to it– Returns that pointer– Main prints twice
The Stack
• C++ allocates variables on a stackint* getPointerToTen() {
int x = 10; int* px = &x; return px;}
int main() { int* pTen = getPointerToTen(); cout << *pTen << endl;}
Address Identifier Value116115114113112111
px 104110109108107
x 10106105104103
pTen ??102101100
The Stack
• C++ allocates variables on a stackint* getPointerToTen() {
int x = 10; int* px = &x; return px;}
int main() { int* pTen = getPointerToTen(); cout << *pTen << endl;}
Address Identifier Value116115114113112111
104110109108107
10106105104103
pTen 104102101100
???
• Pointers to items on stack may go bad
The Stack
• Traditional model:– Stack grows down in memory CODE
GLOBALS
STACK
The Stack
• Traditional model:– Stack grows down in memory• Each function adds a Stack Frame :
new set of local variables
CODE
GLOBALS
STACK
STACK FRAME
STACK FRAME
The Stack
• Traditional model:– Stack grows down in memory• Each function adds a Stack Frame :
new set of local variables• Exiting a function removes a stack
frame
CODE
GLOBALS
STACK
STACK FRAME
The Heap
• The Heap is the extra space– Aka Free Store
• Managed by the OS – C++ functions request parts of
heap from OS
CODE
GLOBALS
STACK
STACK FRAME
HEAP
HEAP
The Heap
• Heap is unaffected by changes to stackCODE
GLOBALS
STACK
STACK
HEAP
HEAP
The Heap
• Heap is unaffected by changes to stackCODE
GLOBALS
STACK
HEAP
HEAPStays until explicitly freed
Dynamic Allocation
• Dynamic Allocation : Allocate space on heap
• Done with new keyword
Address Identifier Value200019991998199719961995199419931992
… … …1000
999998997996995
…
Dynamic Allocation
• Dynamic Allocation : Allocate space on heap
• New returns pointer, must store
Address Identifier Value2000 p 100019991998199719961995199419931992
… … …1000
???999998997996995
…
Values in heap do not have identifiers… must have pointer to them!
Dynamic Allocation
• Dynamic Allocation : Allocate space on heap
• Deference to access
Address Identifier Value2000 p 100019991998199719961995199419931992
… … …1000
100999998997996995
…
Power of Heap
• How will this time be different?
The Stack
int* getGoodPointerToTen() { int* px = new int(10); return px;}
int main() { int* pTen = getPointerToTen(); cout << *pTen << endl;}
Address Identifier Value2000
pTen ???1999199819971996
px 10001995199419931992
… … …1000
10999998997996995
…
The Stack
int* getGoodPointerToTen() { int* px = new int(10); return px;}
int main() { int* pTen = getPointerToTen(); cout << *pTen << endl;}
Address Identifier Value2000
pTen 10001999199819971996
10001995199419931992
… … …1000
10999998997996995
…
Dangers
• Losing track of memory "memory leak"
Address Identifier Value2000 myData 100019961992198819841980197619721968
… … …1000 5996992988984980…
Dangers
• Losing track of memory "memory leak"
• Asked for two ints, only rememberwhere one is!
Address Identifier Value2000 myData 99619961992198819841980197619721968
… … …1000 5996 8992988984980…
Accessing Heap Values
• delete tells OS we are donewith memory
Address Identifier Value2000 myData 100019961992198819841980197619721968
… … …1000 5996992988984980…
Accessing Heap Values
• delete tells OS we are donewith memory
Address Identifier Value2000 myData 100019961992198819841980197619721968
… … …1000 5996992988984980…
Accessing Heap Values
• delete tells OS we are donewith memory
• Nulling pointer prevents usingthat memory
Address Identifier Value2000 myData 019961992198819841980197619721968
… … …1000 5996992988984980…
Malloc / Free
• In C there is no new/delete– Malloc allocates given number of bytes• Returns untyped pointer - cast to desired type
– Free releases memory
Compiler Rules
• Items on stack must be predictable size– Why arrays must be constant size
Compiler Rules
• Items on stack must be predictable size– Why arrays must be constant size
• Items in the heap can be any size at all– Arrays in the heap are flexible
Arrays & Pointers
• Array = memory address of base element
• Pointer = address of item of data
• Largely interchangeable:
Address Identifier Value2000 nums[4] 51996 nums[3] 41992 nums[2] 31988 nums[1] 21984 nums 11980 pToArray 1984197619721968
… … …1000996992988984980…
Dynamic Array
• Array on heap can be variable sized– Store result as a pointer
– Then use that pointer as an array:
Address Identifier Value2000 nums2 98419961992198819841980197619721968
… … …1000 5996 4992 3988 2984 1980…
Returning Dynamic Array
• Returning arrays– Can return array
as pointer– Better be created
on heap!
Deleting Arrays
• Delete with [] to free memoryin an array
Address Identifier Value2000 nums2 98419961992198819841980197619721968
… … …1000 5996 4992 3988 2984 1980…
Deleting Arrays
• Delete with [] to free memoryin an array
Address Identifier Value2000 nums2 98419961992198819841980197619721968
… … …1000 5996 4992 3988 2984 1980…
Deleting Arrays
• Delete with [] to free memoryin an array
Address Identifier Value2000 nums2 019961992198819841980197619721968
… … …1000 5996 4992 3988 2984 1980…
How Do I Use In Project?
• To read in number and make storage must use dynamic memory: