Heap Memory Visualization

int* p = new int(42);
// ...
delete p;

Linked Lists: Building Chains

struct Node {
    int data;
    Node* next;
};

Node* head = nullptr;  // Empty list

Mini‑Quiz Q1

Q1. Which is the correct way to allocate an array of 10 integers in C++?



Mini‑Quiz Q2

Q2. What happens if you forget to `delete` dynamically allocated memory?



Summary

  • Dynamic memory (`new`/`delete`) allows flexible sizing at runtime.
  • Forgetting to `delete` causes memory leaks. Accessing memory after `delete` is undefined behavior.
  • Modern C++ strongly prefers smart pointers (`std::unique_ptr`) to automate memory management and prevent these errors.
  • Linked lists are a fundamental dynamic data structure built with nodes and pointers.