Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dynamic Memory Management

Similar presentations


Presentation on theme: "Dynamic Memory Management"— Presentation transcript:

1 Dynamic Memory Management
CSCE 121

2 Memory Leaks Lose access to memory allocated on the heap.
Easy to lose when dealing with pointers We want to avoid!!! Sometimes conflated with poor memory management.

3 Managing Memory Programmer gets memory from the heap
Programmer must free it when done Commonly referred to as garbage collection. Good exam question What are specific scenarios that can result in a memory leak? Think about the definition of a memory leak as we continue to discuss dynamic memory management.

4 Strategies to Manage Dynamic Memory (i.e. do garbage collection)
Impractical Avoid using dynamic memory. Balance allocation with deallocation. news with deletes Automatic Garbage Collection While these do exist for C++, tend to be inefficient and slow things down. RAII – Resource Allocation is Initialization When possible allocate in the constructor deallocate in the destructor Too broad as a strategy. Inefficient, use a different language. Preferred!

5 Dynamic Classes Classes that use dynamic memory, must implement certain functions to effectively manage memory on the heap. Rule of Three If you implement one of the following, you must implement the other two! Destructor Copy Assignment Copy Constructor Rule of Five (We won’t cover these) Move Assignment Move Constructor We’ll see more on these later. Things will break if you do not implement these! Things will work, but not efficiently!


Download ppt "Dynamic Memory Management"

Similar presentations


Ads by Google