Notes

Thoughts, write-ups, and things worth remembering.

new and delete

#cpp#memory Updated Sep 16, 2026

new allocates on the heap and calls the constructor. delete calls the destructor and frees the memory. They must be paired — every new needs exactly one matching delete.

Node* n = new Node();
delete n;

int* arr = new int[10];
delete[] arr;   // array new needs array delete - mixing these is undefined behavior

Why nobody should write this directly

Forget the delete → leak. delete twice → crash or corrupted heap (see Rule of Five and deep copy). delete and keep using the pointer → dangling pointer, reading/writing memory you no longer own. This entire category of bug is why Smart pointers exist — they call delete for you, automatically, exactly once.

Stack vs heap
Smart pointers