Memory Management - Kamills-12/2143-OOP GitHub Wiki

Memory Management

Kade Miller


What Is Memory Management?

Memory management is how your program uses RAM, allocating space when needed and cleaning it up when you're done. In C++, you do this manually so you’ve gotta stay on top of it.

If you don’t? You get:

  • Memory leaks (you forgot to free memory)
  • Dangling pointers (you freed memory but still point to it)
  • Crashes (accessing memory you don’t own)

Static vs Dynamic Memory:

Type Allocated At Frees Automatically? Example
Stack Compile-time Yes int x = 5;
Heap Runtime No (you handle it) int* x = new int;

Using new and delete

int* ptr = new int;   // allocates space on heap
*ptr = 10;
delete ptr;           // frees the memory