1 / 4

Understanding Local and Dynamic Storage in C++

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.

kera
Télécharger la présentation

Understanding Local and Dynamic Storage in C++

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. Local and Dynamic Storage Bryce Boe 2012/09/04 CS32, Summer 2012 B

  2. Overview • Project 2 Code Overview • Local Variables • Dynamic memory allocation

  3. 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); }

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

More Related