360 likes | 674 Views
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
E N D
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 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 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 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 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 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 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 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 void foo(int q) {if(true) { char c = 'a'; }} int main() {int x = 10;double y = 1.2;foo(5);int z = 5;}
What will this do? • getPointerToTen • Makes a local variable • Makes a pointer to it • Returns that pointer
The Stack • C++ allocates variables on a stack int* getPointerToTen() {intx = 10;int* px = &x; return px;} int main() {int* pTen = getPointerToTen();cout<< *pTen<< endl;}
The Stack • C++ allocates variables on a stack int* getPointerToTen() {intx = 10;int* px = &x; return px;} int main() {int* pTen = getPointerToTen();cout<< *pTen<< endl;}
??? • This is probably enough to bash the stack:
The Stack • Traditional model: • Stack grows down in memory
The Stack • Traditional model: • Stack grows down in memory • Each function adds a Stack Frame :new set of local variables
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 stackframe
The Heap • The Heap is the extra space • Aka Free Store • Managed by the OS in C++ • C++ functions request parts ofheap from OS
The Heap • Heap is unaffected by changes to stack
The Heap • Heap is unaffected by changes to stack • Unless you completely run outof space
Dynamic Allocation • Dynamic Allocation : allocating space for variables in the heap • Done with new keyword • Values in the heap • Do not have identifiers • Sit there until we get rid of them
Accessing Heap Values • Need pointer to access valuesin heap • new returns address of the newvalue on the heap
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;}
The Stack int* getGoodPointerToTen() {int* px = new int(10); return px;} int main() {int* pTen = getPointerToTen();cout<< *pTen<< endl;}
Accessing Heap Values • delete releases memory referenced by a pointer
Accessing Heap Values • delete releases memory referenced by a pointer • NULL out pointers after deleting
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
Dynamic Array • Arrays created with new can be variable size • Store result as a pointer • Then use that pointer as an array:
Returning Dynamic Array • Array • Created in function • Returned as address
Dangers • Losing track of memory "memory leak"
Dangers • We own stuff at 2010 but no longer know where it is • Garbage!
Deleting Arrays • Delete with [] to free memoryin an array • Leak free version
Just delete • All memory your program uses freed on exit • Memory only leaked while your program is running • Not a bad idea to delete data yourself at end of program: