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
void foo(int q) { if(true) { char c = 'a'; } } int main() { int x = 10; double y = 1.2; foo(5); int z = 5; } Address Identifier Value 116 115 114 113 112 111 110 109 108 107 106 105 104 103 102 101 100
3
The Stack C++ allocates variables on a stack
void foo(int q) { if(true) { char c = 'a'; } } int main() { int x = 10; double y = 1.2; foo(5); int z = 5; } Address Identifier Value 116 115 114 113 112 111 110 109 108 107 106 105 104 103 X 10 102 101 100
4
The Stack C++ allocates variables on a stack
void foo(int q) { if(true) { char c = 'a'; } } int main() { int x = 10; double y = 1.2; foo(5); int z = 5; } Address Identifier Value 116 115 114 113 112 111 Y 1.2 110 109 108 107 106 105 104 103 X 10 102 101 100
5
The Stack C++ allocates variables on a stack
void foo(int q) { if(true) { char c = 'a'; } } int main() { int x = 10; double y = 1.2; foo(5); int z = 5; } Address Identifier Value 116 115 q 5 114 113 112 111 Y 1.2 110 109 108 107 106 105 104 103 X 10 102 101 100
6
The Stack C++ allocates variables on a stack
void foo(int q) { if(true) { char c = 'a'; } } int main() { int x = 10; double y = 1.2; foo(5); int z = 5; } Address Identifier Value 116 c a 115 q 5 114 113 112 111 Y 1.2 110 109 108 107 106 105 104 103 X 10 102 101 100
7
The Stack C++ allocates variables on a stack
void foo(int q) { if(true) { char c = 'a'; } } int main() { int x = 10; double y = 1.2; foo(5); int z = 5; } Address Identifier Value 116 115 q 5 114 113 112 111 Y 1.2 110 109 108 107 106 105 104 103 X 10 102 101 100
8
The Stack C++ allocates variables on a stack
void foo(int q) { if(true) { char c = 'a'; } } int main() { int x = 10; double y = 1.2; foo(5); int z = 5; } Address Identifier Value 116 115 114 113 112 111 Y 1.2 110 109 108 107 106 105 104 103 X 10 102 101 100
9
The Stack C++ allocates variables on a stack
void foo(int q) { if(true) { char c = 'a'; } } int main() { int x = 10; double y = 1.2; foo(5); int z = 5; } Address Identifier Value 116 115 z 5 114 113 112 111 Y 1.2 110 109 108 107 106 105 104 103 X 10 102 101 100
10
The Stack C++ allocates variables on a stack
void foo(int q) { if(true) { char c = 'a'; } } int main() { int x = 10; double y = 1.2; foo(5); int z = 5; } Address Identifier Value 116 115 114 113 112 111 110 109 108 107 106 105 104 103 102 101 100
11
What will this do? getPointerToTen Initialize a variable
Makes a pointer to it Returns that pointer Main prints twice
12
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
13
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
14
??? Pointers to items on stack may go bad
15
The Stack Traditional model: Stack grows down in memory CODE GLOBALS
16
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
17
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
18
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
19
The Heap Heap is unaffected by changes to stack CODE GLOBALS STACK
20
The Heap Heap is unaffected by changes to stack
CODE GLOBALS STACK HEAP Stays until explicitly freed
21
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
22
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!
23
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
24
Power of Heap How will this time be different?
25
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
26
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
27
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"
28
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!
29
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
30
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
31
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
32
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
33
Compiler Rules Items on stack must be predictable size
Why arrays must be constant size
34
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
35
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
36
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
37
Returning Dynamic Array
Returning arrays Can return array as pointer Better be created on heap!
38
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];
39
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];
40
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];
41
How Do I Use In Project? To read in number and make storage must use dynamic memory:
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.