Download presentation
Presentation is loading. Please wait.
1
Dynamic Memory A whole heap of fun…
2
What will this do? getPointerToTen Initialize a variable
Makes a pointer to it Returns that pointer Main prints twice
3
The Stack C++ allocates variables on a stack 1000 pTen ??
int* getBadPointer() { int x = 10; int* px = &x; return px; } void foo() { int z = 5; int main(){ int* pTen = getBadPointer(); cout << *pTen << endl; foo(); return 0; Address Identifier Value 1000 pTen ??
4
The Stack C++ allocates variables on a stack 1008 px ?? 1004 x 1000
int* getBadPointer() { int x = 10; int* px = &x; return px; } void foo() { int z = 5; int main(){ int* pTen = getBadPointer(); cout << *pTen << endl; foo(); return 0; Address Identifier Value 1008 px ?? 1004 x 1000 pTen
5
The Stack C++ allocates variables on a stack 1008 px 1004 x 10 1000
int* getBadPointer() { int x = 10; int* px = &x; return px; } void foo() { int z = 5; int main(){ int* pTen = getBadPointer(); cout << *pTen << endl; foo(); return 0; Address Identifier Value 1008 px 1004 x 10 1000 pTen ??
6
The Stack C++ allocates variables on a stack 1008 1004 10 1000 pTen
int* getBadPointer() { int x = 10; int* px = &x; return px; } void foo() { int z = 5; int main(){ int* pTen = getBadPointer(); cout << *pTen << endl; foo(); return 0; Address Identifier Value 1008 1004 10 1000 pTen
7
The Stack C++ allocates variables on a stack 1008 1004 10 1000 pTen
int* getBadPointer() { int x = 10; int* px = &x; return px; } void foo() { int z = 5; int main(){ int* pTen = getBadPointer(); cout << *pTen << endl; //10 foo(); cout << *pTen << endl; return 0; Address Identifier Value 1008 1004 10 1000 pTen
8
The Stack C++ allocates variables on a stack 1008 1004 z 10 1000 pTen
int* getBadPointer() { int x = 10; int* px = &x; return px; } void foo() { int z = 5; int main(){ int* pTen = getBadPointer(); cout << *pTen << endl; //10 foo(); cout << *pTen << endl; return 0; Address Identifier Value 1008 1004 z 10 1000 pTen
9
The Stack C++ allocates variables on a stack 1008 1004 z 5 1000 pTen
int* getBadPointer() { int x = 10; int* px = &x; return px; } void foo() { int z = 5; int main(){ int* pTen = getBadPointer(); cout << *pTen << endl; //10 foo(); cout << *pTen << endl; return 0; Address Identifier Value 1008 1004 z 5 1000 pTen
10
The Stack C++ allocates variables on a stack 1008 1004 5 1000 pTen
int* getBadPointer() { int x = 10; int* px = &x; return px; } void foo() { int z = 5; int main(){ int* pTen = getBadPointer(); cout << *pTen << endl; //10 foo(); cout << *pTen << endl; return 0; Address Identifier Value 1008 1004 5 1000 pTen
11
The Stack C++ allocates variables on a stack 1008 1004 5 1000 pTen
int* getBadPointer() { int x = 10; int* px = &x; return px; } void foo() { int z = 5; int main(){ int* pTen = getBadPointer(); cout << *pTen << endl; //10 foo(); cout << *pTen << endl; //5 return 0; Address Identifier Value 1008 1004 5 1000 pTen
12
The Stack Traditional model: Stack grows down in memory CODE GLOBALS
13
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
14
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
15
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
16
The Heap Heap is unaffected by changes to stack CODE GLOBALS STACK
17
The Heap Heap is unaffected by changes to stack
CODE GLOBALS STACK HEAP Stays until explicitly freed
18
Dynamic Allocation Dynamic Allocation : Allocate space on heap
Done with new keyword Address Identifier Value 2000 1996 1992 1988 1984 1980 1976 … 1000 999 998 997 996 995
19
Dynamic Allocation Dynamic Allocation : Allocate space on heap
New returns pointer, must store Address Identifier Value 2000 p 1000 1996 1992 1988 1984 1980 1976 … ??? 999 998 997 996 995 Values in heap do not have identifiers… must have pointer to them!
20
Dynamic Allocation Dynamic Allocation : Allocate space on heap
Deference to access Address Identifier Value 2000 p 1000 1996 1992 1988 1984 1980 1976 … 100 999 998 997 996 995
21
Dynamic Allocation Address Identifier Value 2000 p 1000 1996 1992 1988 1984 1980 1976 … 100 999 998 997 996 995 Initalize heap memory as it is allocated with type(value)
22
Power of Heap How will this time be different?
23
The Stack int* getGoodPointerToTen() { int* px = new int(10); return px; } void foo() { int z = 5; int main() { int* pTen = getGoodPointerToTen(); cout << *pTen << endl; foo(); Address Identifier Value 2000 pTen ?? 1996 1992 1988 1984 1980 1976 … 1000 999 998 997 996 995
24
The Stack int* getGoodPointerToTen() { int* px = new int(10); return px; } void foo() { int z = 5; int main() { int* pTen = getGoodPointerToTen(); cout << *pTen << endl; foo(); Address Identifier Value 2000 pTen ?? 1996 px 1992 1988 1984 1980 1976 … 1000 999 998 997 996 995
25
The Stack int* getGoodPointerToTen() { int* px = new int(10); return px; } void foo() { int z = 5; int main() { int* pTen = getGoodPointerToTen(); cout << *pTen << endl; foo(); Address Identifier Value 2000 pTen ?? 1996 px 1992 1988 1984 1980 1976 … 1000 10 999 998 997 996 995
26
The Stack int* getGoodPointerToTen() { int* px = new int(10); return px; } void foo() { int z = 5; int main() { int* pTen = getGoodPointerToTen(); cout << *pTen << endl; foo(); Address Identifier Value 2000 pTen ?? 1996 px 1000 1992 1988 1984 1980 1976 … 10 999 998 997 996 995
27
The Stack int* getGoodPointerToTen() { int* px = new int(10); return px; } void foo() { int z = 5; int main() { int* pTen = getGoodPointerToTen(); cout << *pTen << endl; foo(); Address Identifier Value 2000 pTen 1000 1996 1992 1988 1984 1980 1976 … 10 999 998 997 996 995
28
The Stack int* getGoodPointerToTen() { int* px = new int(10); return px; } void foo() { int z = 5; int main() { int* pTen = getGoodPointerToTen(); cout << *pTen << endl; //10 foo(); cout << *pTen << endl; Address Identifier Value 2000 pTen 1000 1996 1992 1988 1984 1980 1976 … 10 999 998 997 996 995
29
The Stack int* getGoodPointerToTen() { int* px = new int(10); return px; } void foo() { int z = 5; int main() { int* pTen = getGoodPointerToTen(); cout << *pTen << endl; //10 foo(); cout << *pTen << endl; Address Identifier Value 2000 pTen 1000 1996 z 1992 1988 1984 1980 1976 … 10 999 998 997 996 995
30
The Stack int* getGoodPointerToTen() { int* px = new int(10); return px; } void foo() { int z = 5; int main() { int* pTen = getGoodPointerToTen(); cout << *pTen << endl; //10 foo(); cout << *pTen << endl; Address Identifier Value 2000 pTen 1000 1996 z 5 1992 1988 1984 1980 1976 … 10 999 998 997 996 995
31
The Stack int* getGoodPointerToTen() { int* px = new int(10); return px; } void foo() { int z = 5; int main() { int* pTen = getGoodPointerToTen(); cout << *pTen << endl; //10 foo(); cout << *pTen << endl; Address Identifier Value 2000 pTen 1000 1996 5 1992 1988 1984 1980 1976 … 10 999 998 997 996 995
32
The Stack int* getGoodPointerToTen() { int* px = new int(10); return px; } void foo() { int z = 5; int main() { int* pTen = getGoodPointerToTen(); cout << *pTen << endl; //10 foo(); Address Identifier Value 2000 pTen 1000 1996 5 1992 1988 1984 1980 1976 … 10 999 998 997 996 995
33
Leaks
34
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"
35
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!
36
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
37
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
38
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.