Download presentation
Presentation is loading. Please wait.
1
Dynamic Memory A whole heap of fun…
2
Review: The Stack C++ allocates variables on a stack
All memory that might be used in function allocated at one time Address Identifier Value 116 115 114 113 112 111 110 109 108 107 106 105 104 103 102 101 100
3
Review: The Stack void foo(int q) { if(true) { char c = 'a'; } } void bar() { int x = 8; } int main() { int x = 10; int y = 1; foo(6); int z = 5; bar(); } Address Identifier Value 116 115 114 113 112 111 110 109 108 107 106 105 104 103 102 101 100
4
Review: The Stack void foo(int q) { if(true) { char c = 'a'; } } void bar() { int x = 8; } int main() { int x = 10; int y = 1; foo(x); int z = 5; bar(); } Address Identifier Value 116 ?? 115 114 113 112 111 z 110 109 108 107 y 106 105 104 103 X 102 101 100
5
Review: The Stack void foo(int q) { if(true) { char c = 'a'; } } void bar() { int x = 8; } int main() { int x = 10; int y = 1; foo(x); int z = 5; bar(); } Address Identifier Value 116 ?? 115 114 113 112 111 z 110 109 108 107 y 106 105 104 103 X 10 102 101 100
6
Review: The Stack void foo(int q) { if(true) { char c = 'a'; } } void bar() { int x = 8; } int main() { int x = 10; int y = 1; foo(x); int z = 5; bar(); } Address Identifier Value 116 ?? 115 114 113 112 111 z 110 109 108 107 y 1 106 105 104 103 X 10 102 101 100
7
Review: The Stack void foo(int q) { if(true) { char c = 'a'; } } void bar() { int x = 8; } int main() { int x = 10; int y = 1; foo(x); int z = 5; bar(); } Address Identifier Value 116 c ?? 115 q 10 114 113 112 111 z 110 109 108 107 y 1 106 105 104 103 X 102 101 100
8
Review: The Stack void foo(int q) { if(true) { char c = 'a'; } } void bar() { int x = 8; } int main() { int x = 10; int y = 1; foo(x); int z = 5; bar(); } Address Identifier Value 116 c 'a' 115 q 10 114 113 112 111 z ?? 110 109 108 107 y 1 106 105 104 103 X 102 101 100
9
Review: The Stack void foo(int q) { if(true) { char c = 'a'; } } void bar() { int x = 8; } int main() { int x = 10; int y = 1; foo(x); int z = 5; bar(); } Address Identifier Value 116 'a' 115 10 114 113 112 111 z ?? 110 109 108 107 y 1 106 105 104 103 X 102 101 100
10
Review: The Stack void foo(int q) { if(true) { char c = 'a'; } } void bar() { int x = 8; } int main() { int x = 10; int y = 1; foo(x); int z = 5; bar(); } Address Identifier Value 116 'a' 115 10 114 113 112 111 z 5 110 109 108 107 y 1 106 105 104 103 X 102 101 100
11
Review: The Stack void foo(int q) { if(true) { char c = 'a'; } } void bar() { int x = 8; } int main() { int x = 10; int y = 1; foo(x); int z = 5; bar(); } Address Identifier Value 116 'a' 115 x 10 114 113 112 111 z 5 110 109 108 107 y 1 106 105 104 103 X 102 101 100
12
Review: The Stack void foo(int q) { if(true) { char c = 'a'; } } void bar() { int x = 8; } int main() { int x = 10; int y = 1; foo(x); int z = 5; bar(); } Address Identifier Value 116 'a' 115 x 8 114 113 112 111 z 5 110 109 108 107 y 1 106 105 104 103 X 10 102 101 100
13
Review: The Stack void foo(int q) { if(true) { char c = 'a'; } } void bar() { int x = 8; } int main() { int x = 10; int y = 1; foo(x); int z = 5; bar(); } Address Identifier Value 116 'a' 115 8 114 113 112 111 z 5 110 109 108 107 y 1 106 105 104 103 X 10 102 101 100
14
Review: The Stack void foo(int q) { if(true) { char c = 'a'; } } void bar() { int x = 8; } int main() { int x = 10; int y = 1; foo(x); int z = 5; bar(); } Address Identifier Value 116 'a' 115 8 114 113 112 111 5 110 109 108 107 1 106 105 104 103 10 102 101 100
15
What will this do? getPointerToTen Initialize a variable
Makes a pointer to it Returns that pointer Main prints twice
16
The Stack C++ allocates variables on a stack
int* getPointerToTen() { int x = 10; int* px = &x; return px; } int main() { int* pTen = getPointerToTen(); cout << *pTen << endl; } Address Identifier Value 116 115 114 113 112 111 110 109 108 107 106 105 104 103 pTen ?? 102 101 100
17
The Stack C++ allocates variables on a stack
int* getPointerToTen() { int x = 10; int* px = &x; return px; } int main() { int* pTen = getPointerToTen(); cout << *pTen << endl; } Address Identifier Value 116 115 114 113 112 111 px 104 110 109 108 107 x 10 106 105 103 pTen ?? 102 101 100
18
The Stack C++ allocates variables on a stack
int* getPointerToTen() { int x = 10; int* px = &x; return px; } int main() { int* pTen = getPointerToTen(); cout << *pTen << endl; } Address Identifier Value 116 115 114 113 112 111 104 110 109 108 107 10 106 105 103 pTen 102 101 100
19
??? Pointers to items on stack may go bad
20
The Stack Traditional model: Stack grows down in memory CODE GLOBALS
21
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
22
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
23
The Heap The Heap is the extra space Managed by the OS Aka Free Store
C++ functions request parts of heap from OS CODE GLOBALS STACK STACK FRAME HEAP
24
The Heap Heap is unaffected by changes to stack CODE GLOBALS STACK
25
The Heap Heap is unaffected by changes to stack
CODE GLOBALS STACK HEAP Stays until explicitly freed
26
Dynamic Allocation Dynamic Allocation : Allocate space on heap
Done with new keyword Address Identifier Value 2000 1999 1998 1997 1996 1995 1994 1993 1992 … 1000 999 998 997 996 995
27
Dynamic Allocation Dynamic Allocation : Allocate space on heap
New returns pointer, must store Address Identifier Value 2000 p 1000 1999 1998 1997 1996 1995 1994 1993 1992 … ??? 999 998 997 996 995 Values in heap do not have identifiers… must have pointer to them!
28
Dynamic Allocation Dynamic Allocation : Allocate space on heap
Deference to access Address Identifier Value 2000 p 1000 1999 1998 1997 1996 1995 1994 1993 1992 … 100 999 998 997 996 995
29
Power of Heap How will this time be different?
30
The Stack int* getGoodPointerToTen() { int* px = new int(10); return px; } int main() { int* pTen = getPointerToTen(); cout << *pTen << endl; } Address Identifier Value 2000 pTen ??? 1999 1998 1997 1996 px 1000 1995 1994 1993 1992 … 10 999 998 997 996 995
31
The Stack int* getGoodPointerToTen() { int* px = new int(10); return px; } int main() { int* pTen = getPointerToTen(); cout << *pTen << endl; } Address Identifier Value 2000 pTen 1000 1999 1998 1997 1996 1995 1994 1993 1992 … 10 999 998 997 996 995
32
Dangers Losing track of memory "memory leak" 2000 myData 1000 1996
Address Identifier Value 2000 myData 1000 1996 1992 1988 1984 1980 1976 1972 1968 … 5 996 992 988 984 980 Losing track of memory "memory leak"
33
Dangers Losing track of memory "memory leak"
Address Identifier Value 2000 myData 996 1996 1992 1988 1984 1980 1976 1972 1968 … 1000 5 8 992 988 984 980 Losing track of memory "memory leak" Asked for two ints, only remember where one is!
34
Accessing Heap Values delete tells OS we are done with memory 2000
Address Identifier Value 2000 myData 1000 1996 1992 1988 1984 1980 1976 1972 1968 … 5 996 992 988 984 980 delete tells OS we are done with memory
35
Accessing Heap Values delete tells OS we are done with memory 2000
Address Identifier Value 2000 myData 1000 1996 1992 1988 1984 1980 1976 1972 1968 … 5 996 992 988 984 980 delete tells OS we are done with memory
36
Accessing Heap Values delete tells OS we are done with memory
Address Identifier Value 2000 myData 1996 1992 1988 1984 1980 1976 1972 1968 … 1000 5 996 992 988 984 980 delete tells OS we are done with memory Nulling pointer prevents using that memory
37
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
38
Compiler Rules Items on stack must be predictable size
Why arrays must be constant size
39
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
40
Arrays & Pointers Array = memory address of base element
Pointer = address of item of data Largely interchangeable: Address Identifier Value 2000 nums[4] 5 1996 nums[3] 4 1992 nums[2] 3 1988 nums[1] 2 1984 nums 1 1980 pToArray 1976 1972 1968 … 1000 996 992 988 984 980
41
Dynamic Array Array on heap can be variable sized
Store result as a pointer Then use that pointer as an array: Address Identifier Value 2000 nums2 984 1996 1992 1988 1984 1980 1976 1972 1968 … 1000 5 996 4 992 3 988 2 1 980
42
Returning Dynamic Array
Returning arrays Can return array as pointer Better be created on heap!
43
Deleting Arrays Delete with [] to free memory in an array 2000 nums2
Address Identifier Value 2000 nums2 984 1996 1992 1988 1984 1980 1976 1972 1968 … 1000 5 996 4 992 3 988 2 1 980 } int* nums = new int[listSize]; //do stuff... //delete old array - free that memory delete [] nums; //allocate new array and use nums to point to it nums = new int[listSize];
44
Deleting Arrays Delete with [] to free memory in an array 2000 nums2
Address Identifier Value 2000 nums2 984 1996 1992 1988 1984 1980 1976 1972 1968 … 1000 5 996 4 992 3 988 2 1 980 } int* nums = new int[listSize]; //do stuff... //delete old array - free that memory delete [] nums; //allocate new array and use nums to point to it nums = new int[listSize];
45
Deleting Arrays Delete with [] to free memory in an array 2000 nums2
Address Identifier Value 2000 nums2 1996 1992 1988 1984 1980 1976 1972 1968 … 1000 5 996 4 992 3 988 2 984 1 980 } int* nums = new int[listSize]; //do stuff... //delete old array - free that memory delete [] nums; //allocate new array and use nums to point to it nums = new int[listSize];
46
How Do I Use In Project? To read in number and make storage must use dynamic memory:
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.