50 likes | 179 Vues
This guide delves into the concepts of local and dynamic memory storage in C++. It outlines how local variables are stored in a function's activation record on the stack, providing an example of a countdown function. Dynamic memory allocation is discussed, explaining how variables are stored in the heap, allocated using `malloc` or `new`, and deallocated with `free` or `delete`. It also covers memory management by the OS and the importance of pointers in accessing allocated resources.
E N D
Local and Dynamic Storage Bryce Boe 2012/09/04 CS32, Summer 2012 B
Overview • Project 2 Code Overview • Local Variables • Dynamic memory allocation
Local Variables • Local variables are stored as part of a function’s activation record on the stack void count_down(int n) { cout << n << “ “; if (n > 0) count_down(n – 1); }
Dynamic Variables • Stored in the heap • Allocated via malloc-family or new • Deallocated via free or delete • Process’s memory management (provided by libc) maintains free lists • Requests more memory from the OS as necessary • Requires a local or static variable to point to the allocated resource