1 / 36

Dynamic Memory

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

shelby
Télécharger la présentation

Dynamic Memory

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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;}

  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;}

  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;}

  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;}

  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;}

  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;}

  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;}

  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;}

  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;}

  11. What will this do? • getPointerToTen • Makes a local variable • Makes a pointer to it • Returns that pointer

  12. 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;}

  13. 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;}

  14. ??? • This is probably enough to bash the stack:

  15. The Stack • Traditional model: • Stack grows down in memory

  16. The Stack • Traditional model: • Stack grows down in memory • Each function adds a Stack Frame :new set of local variables

  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 stackframe

  18. The Heap • The Heap is the extra space • Aka Free Store • Managed by the OS in C++ • C++ functions request parts ofheap from OS

  19. The Heap • Heap is unaffected by changes to stack

  20. The Heap • Heap is unaffected by changes to stack • Unless you completely run outof space

  21. 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

  22. Accessing Heap Values • Need pointer to access valuesin heap • new returns address of the newvalue on the heap

  23. Power of Heap • How will this time be different?

  24. The Stack int* getGoodPointerToTen() { int* px = new int(10); return px;} int main() {int* pTen = getPointerToTen(); cout << *pTen << endl;}

  25. The Stack int* getGoodPointerToTen() {int* px = new int(10); return px;} int main() {int* pTen = getPointerToTen();cout<< *pTen<< endl;}

  26. Accessing Heap Values • delete releases memory referenced by a pointer

  27. Accessing Heap Values • delete releases memory referenced by a pointer • NULL out pointers after deleting

  28. 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

  29. Compiler Rules • Items on stack must be predictable size • Why arrays must be constant size

  30. 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

  31. Dynamic Array • Arrays created with new can be variable size • Store result as a pointer • Then use that pointer as an array:

  32. Returning Dynamic Array • Array • Created in function • Returned as address

  33. Dangers • Losing track of memory "memory leak"

  34. Dangers • We own stuff at 2010 but no longer know where it is • Garbage!

  35. Deleting Arrays • Delete with [] to free memoryin an array • Leak free version

  36. 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:

More Related