What happens under the hood when you call delete vs. delete[]? Why is it undefined behavior to mix them?
Last updated
deleteruns one destructor then frees.delete[]destroys every element, often using an element count (cookie) stored bynew[]just before the pointer.Mixing them is UB: skipped destructors, freeing from the wrong address, or reading a nonexistent count, which can corrupt the heap.
Always match the form of
new, or avoid the choice withstd::vectororstd::unique_ptr<T[]>.