dynamic memory
DESCRIPTION
Dynamic Memory. A whole heap of fun…. 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; }. The Stack. C++ allocates variables on a stack - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Dynamic Memory](https://reader036.vdocument.in/reader036/viewer/2022062314/56814467550346895db0fad0/html5/thumbnails/1.jpg)
Dynamic Memory A whole heap of fun…
![Page 2: Dynamic Memory](https://reader036.vdocument.in/reader036/viewer/2022062314/56814467550346895db0fad0/html5/thumbnails/2.jpg)
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
![Page 3: Dynamic Memory](https://reader036.vdocument.in/reader036/viewer/2022062314/56814467550346895db0fad0/html5/thumbnails/3.jpg)
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
![Page 4: Dynamic Memory](https://reader036.vdocument.in/reader036/viewer/2022062314/56814467550346895db0fad0/html5/thumbnails/4.jpg)
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
![Page 5: Dynamic Memory](https://reader036.vdocument.in/reader036/viewer/2022062314/56814467550346895db0fad0/html5/thumbnails/5.jpg)
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
![Page 6: Dynamic Memory](https://reader036.vdocument.in/reader036/viewer/2022062314/56814467550346895db0fad0/html5/thumbnails/6.jpg)
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
![Page 7: Dynamic Memory](https://reader036.vdocument.in/reader036/viewer/2022062314/56814467550346895db0fad0/html5/thumbnails/7.jpg)
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
![Page 8: Dynamic Memory](https://reader036.vdocument.in/reader036/viewer/2022062314/56814467550346895db0fad0/html5/thumbnails/8.jpg)
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
![Page 9: Dynamic Memory](https://reader036.vdocument.in/reader036/viewer/2022062314/56814467550346895db0fad0/html5/thumbnails/9.jpg)
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
![Page 10: Dynamic Memory](https://reader036.vdocument.in/reader036/viewer/2022062314/56814467550346895db0fad0/html5/thumbnails/10.jpg)
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
![Page 11: Dynamic Memory](https://reader036.vdocument.in/reader036/viewer/2022062314/56814467550346895db0fad0/html5/thumbnails/11.jpg)
What will this do?
• getPointerToTen – Initialize a variable– Makes a pointer to it– Returns that pointer– Main prints twice
![Page 12: Dynamic Memory](https://reader036.vdocument.in/reader036/viewer/2022062314/56814467550346895db0fad0/html5/thumbnails/12.jpg)
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
![Page 13: Dynamic Memory](https://reader036.vdocument.in/reader036/viewer/2022062314/56814467550346895db0fad0/html5/thumbnails/13.jpg)
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
![Page 14: Dynamic Memory](https://reader036.vdocument.in/reader036/viewer/2022062314/56814467550346895db0fad0/html5/thumbnails/14.jpg)
???
• Pointers to items on stack may go bad
![Page 15: Dynamic Memory](https://reader036.vdocument.in/reader036/viewer/2022062314/56814467550346895db0fad0/html5/thumbnails/15.jpg)
The Stack
• Traditional model:– Stack grows down in memory CODE
GLOBALS
STACK
![Page 16: Dynamic Memory](https://reader036.vdocument.in/reader036/viewer/2022062314/56814467550346895db0fad0/html5/thumbnails/16.jpg)
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
![Page 17: Dynamic Memory](https://reader036.vdocument.in/reader036/viewer/2022062314/56814467550346895db0fad0/html5/thumbnails/17.jpg)
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
![Page 18: Dynamic Memory](https://reader036.vdocument.in/reader036/viewer/2022062314/56814467550346895db0fad0/html5/thumbnails/18.jpg)
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
![Page 19: Dynamic Memory](https://reader036.vdocument.in/reader036/viewer/2022062314/56814467550346895db0fad0/html5/thumbnails/19.jpg)
The Heap
• Heap is unaffected by changes to stackCODE
GLOBALS
STACK
STACK
HEAP
HEAP
![Page 20: Dynamic Memory](https://reader036.vdocument.in/reader036/viewer/2022062314/56814467550346895db0fad0/html5/thumbnails/20.jpg)
The Heap
• Heap is unaffected by changes to stackCODE
GLOBALS
STACK
HEAP
HEAPStays until explicitly freed
![Page 21: Dynamic Memory](https://reader036.vdocument.in/reader036/viewer/2022062314/56814467550346895db0fad0/html5/thumbnails/21.jpg)
Dynamic Allocation
• Dynamic Allocation : Allocate space on heap
• Done with new keyword
Address Identifier Value200019991998199719961995199419931992
… … …1000
999998997996995
…
![Page 22: Dynamic Memory](https://reader036.vdocument.in/reader036/viewer/2022062314/56814467550346895db0fad0/html5/thumbnails/22.jpg)
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!
![Page 23: Dynamic Memory](https://reader036.vdocument.in/reader036/viewer/2022062314/56814467550346895db0fad0/html5/thumbnails/23.jpg)
Dynamic Allocation
• Dynamic Allocation : Allocate space on heap
• Deference to access
Address Identifier Value2000 p 100019991998199719961995199419931992
… … …1000
100999998997996995
…
![Page 24: Dynamic Memory](https://reader036.vdocument.in/reader036/viewer/2022062314/56814467550346895db0fad0/html5/thumbnails/24.jpg)
Power of Heap
• How will this time be different?
![Page 25: Dynamic Memory](https://reader036.vdocument.in/reader036/viewer/2022062314/56814467550346895db0fad0/html5/thumbnails/25.jpg)
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
…
![Page 26: Dynamic Memory](https://reader036.vdocument.in/reader036/viewer/2022062314/56814467550346895db0fad0/html5/thumbnails/26.jpg)
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
…
![Page 27: Dynamic Memory](https://reader036.vdocument.in/reader036/viewer/2022062314/56814467550346895db0fad0/html5/thumbnails/27.jpg)
Dangers
• Losing track of memory "memory leak"
Address Identifier Value2000 myData 100019961992198819841980197619721968
… … …1000 5996992988984980…
![Page 28: Dynamic Memory](https://reader036.vdocument.in/reader036/viewer/2022062314/56814467550346895db0fad0/html5/thumbnails/28.jpg)
Dangers
• Losing track of memory "memory leak"
• Asked for two ints, only rememberwhere one is!
Address Identifier Value2000 myData 99619961992198819841980197619721968
… … …1000 5996 8992988984980…
![Page 29: Dynamic Memory](https://reader036.vdocument.in/reader036/viewer/2022062314/56814467550346895db0fad0/html5/thumbnails/29.jpg)
Accessing Heap Values
• delete tells OS we are donewith memory
Address Identifier Value2000 myData 100019961992198819841980197619721968
… … …1000 5996992988984980…
![Page 30: Dynamic Memory](https://reader036.vdocument.in/reader036/viewer/2022062314/56814467550346895db0fad0/html5/thumbnails/30.jpg)
Accessing Heap Values
• delete tells OS we are donewith memory
Address Identifier Value2000 myData 100019961992198819841980197619721968
… … …1000 5996992988984980…
![Page 31: Dynamic Memory](https://reader036.vdocument.in/reader036/viewer/2022062314/56814467550346895db0fad0/html5/thumbnails/31.jpg)
Accessing Heap Values
• delete tells OS we are donewith memory
• Nulling pointer prevents usingthat memory
Address Identifier Value2000 myData 019961992198819841980197619721968
… … …1000 5996992988984980…
![Page 32: Dynamic Memory](https://reader036.vdocument.in/reader036/viewer/2022062314/56814467550346895db0fad0/html5/thumbnails/32.jpg)
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
![Page 33: Dynamic Memory](https://reader036.vdocument.in/reader036/viewer/2022062314/56814467550346895db0fad0/html5/thumbnails/33.jpg)
Compiler Rules
• Items on stack must be predictable size– Why arrays must be constant size
![Page 34: Dynamic Memory](https://reader036.vdocument.in/reader036/viewer/2022062314/56814467550346895db0fad0/html5/thumbnails/34.jpg)
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
![Page 35: Dynamic Memory](https://reader036.vdocument.in/reader036/viewer/2022062314/56814467550346895db0fad0/html5/thumbnails/35.jpg)
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…
![Page 36: Dynamic Memory](https://reader036.vdocument.in/reader036/viewer/2022062314/56814467550346895db0fad0/html5/thumbnails/36.jpg)
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…
![Page 37: Dynamic Memory](https://reader036.vdocument.in/reader036/viewer/2022062314/56814467550346895db0fad0/html5/thumbnails/37.jpg)
Returning Dynamic Array
• Returning arrays– Can return array
as pointer– Better be created
on heap!
![Page 38: Dynamic Memory](https://reader036.vdocument.in/reader036/viewer/2022062314/56814467550346895db0fad0/html5/thumbnails/38.jpg)
Deleting Arrays
• Delete with [] to free memoryin an array
Address Identifier Value2000 nums2 98419961992198819841980197619721968
… … …1000 5996 4992 3988 2984 1980…
![Page 39: Dynamic Memory](https://reader036.vdocument.in/reader036/viewer/2022062314/56814467550346895db0fad0/html5/thumbnails/39.jpg)
Deleting Arrays
• Delete with [] to free memoryin an array
Address Identifier Value2000 nums2 98419961992198819841980197619721968
… … …1000 5996 4992 3988 2984 1980…
![Page 40: Dynamic Memory](https://reader036.vdocument.in/reader036/viewer/2022062314/56814467550346895db0fad0/html5/thumbnails/40.jpg)
Deleting Arrays
• Delete with [] to free memoryin an array
Address Identifier Value2000 nums2 019961992198819841980197619721968
… … …1000 5996 4992 3988 2984 1980…
![Page 41: Dynamic Memory](https://reader036.vdocument.in/reader036/viewer/2022062314/56814467550346895db0fad0/html5/thumbnails/41.jpg)
How Do I Use In Project?
• To read in number and make storage must use dynamic memory: