Skip to content

Mixing up delete and delete[]

Question

Explain the difference between delete and delete[] in C++, and what happens if you use the wrong one.

Short answer

delete frees one object from new; delete[] frees an array from new[], calling every element's destructor. Mismatching them is undefined behaviour: typically only the first destructor runs, or the allocator is handed the wrong address, corrupting the heap or crashing. Prefer std::vector and smart pointers so you never write either.

Adapted from Joshi, Denson & Downes, Quant Job Interview Questions and Answers, Q7.22, p.227; question and answer rewritten by SelfMadeQuant.com.

See the hint and the full worked solution in the question bank (free account).

Open the worked solution

Lessons behind this question