1 / 9

Overview of Memory Management

Overview of Memory Management. “The Big Picture” Memory layout and its overall structure Stack vs. Heap memory management Function call stack is managed automatically Dynamic allocation you manage yourself Example code with operators new, delete Summary of Memory Management.

Download Presentation

Overview of Memory Management

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. Overview of Memory Management • “The Big Picture” • Memory layout and its overall structure • Stack vs. Heap memory management • Function call stack is managed automatically • Dynamic allocation you manage yourself • Example code with operators new, delete • Summary of Memory Management

  2. The “Big” Picture Text (shared) Initialized Data Unitialized Data Heap (Dynamic) stack (dynamic) Environment (Thanks to Prof. Fred Kuhns for this diagram) CPU Unitialized data Stack & heap operations DRAM Allocated virtual pages Low Address (0x00000000) Swap app1 address space Disk UFS High Address (0x7fffffff) Text and initialized data app1 • Each application process gets its own “address space” • A range of protected, relative memory locations that belong to it • Includes global and text (also known as code) segments • CPU manipulates stack and heap as it executes instructions

  3. Your Program’s Address Space Text (shared) Initialized Data Unitialized Data Heap (Dynamic) Environment (Thanks to Prof. Fred Kuhns for this diagram) Code : int main(){…} MyClass::foo(){…} Low Address (0x00000000) Global : int x = 4; Global : int y; Process Address space Heap : dynamically allocated (allocate with new/malloc) Stack : local variables, function parameters stack (dynamic) High Address (0x7fffffff) • Four major memory segments: • Global data, text (also known as code), heap, stack

  4. The program call stack records function calls Keeps a stack of “frames” – one frame per function call Keeps values of local variables/objects in each frame Passes parameter values/references from caller Passes return values/references back to caller Stack frames “pop” (and contents are destroyed) on return The program heap provides additional memory Allocated “as needed” (dynamically) to a running program Operator new obtains memory from the heap And returns a typed pointer to the dynamically allocated memory May perform initialization before returning value, if value given Can also just initialize a given previously allocated location Must release allocated memory with operator delete Stack vs. Dynamic Allocation

  5. #include <iostream> // For standard output stream and manipulators. #include <new> // For allocation exceptions and other symbols. using namespace std; int global; // only very rarely is this a good idea int main (int, char *[]) { int * i = 0; int * j = 0; int * k = 0; try { i = new int (3); // initialization with allocation j = new int; k = new int [2]; // array allocation (uninitialized) cout << "i is " << i << ", j is " << j << ", k is " << k << ", *i is " << *i << ", *j is " << *j << ", k[0] is " << k[0] << ", k[1] is " << k[1] << endl; // sample output: i is 0x94e6008, j is 0x94e6018, k is 0x94e6028, // *i is 3, *j is 0, k[0] is 0, k[1] is 0 } catch (bad_alloc & bad_alloc_exception){} Operator New

  6. delete i; delete j; delete [] k; cout << "i is now " << i << ", j is now " << j << ", k is now " << k << endl; // sample output: i is now 0x94e6008, j is now 0x94e6018, k is now 0x94e6028 i = new (nothrow) int [2]; cout << "i is now " << i << endl; // sample output: i is now 0x94e6028 if (i != 0) { cout << " i[0] is " << i[0] << ", i[1] is " << i[1] << endl; // sample output: i[0] is 156131344, i[1] is 0 // use placement new to set i[0] to 5 and i[1] to 7 j = new (i) int (5); k = new (i+1) int (7); cout << "i[0] is now " << i[0] << ", i[1] is now " << i[1] << endl; // sample output: i[0] is now 5, i[1] is now 7 } delete [] i; return 0; } Operator Delete, Nothrow, Placement New

  7. Understand what parts of a program are located in each of the 4 major memory segments Global data, text (code), heap, stack This tells you a lot about their scope, and lifetime Understand the mechanics of stack vs. dynamic memory allocation Heap and stack both protected by the operating system Both grow toward each other, and can run out of room Summary of Memory Management

  8. Understand how to use new and delete operators Match every new with a corresponding delete Match every new [] with a corresponding delete [] Don’t mix new with delete [] or new [] with delete Use std::nothrow to prevent new from throwing exception But then you must check the return value of the call to new is != 0 Or, initialize pointer to 0, open try block, allocate, catch exception Dynamically allocated memory contains whatever was sitting at that location due to earlier program operations Use explicit allocation if you know values to use when allocating Use “placement new” to initialize allocated memory locations We’ll use both stack and heap memory from now on Especially when we talk about classes and objects Memory Management Summary, Cont.

  9. For Next Week (Tue and Thu Lectures) • Topic: C++ Classes • Assigned Readings • pages 355-358 • pages 445-499 • pages 515-517 • pages 561-631 • pages 787-801

More Related