Why must new/delete and new[]/delete[] be paired correctly, and which RAII alternatives avoid the issue?
Show answer and explanation
A pointer returned by an ordinary new-expression must be passed to the matching single-object delete form, while storage from a new[] expression requires delete[]. The array form destroys the constructed elements and invokes the matching deallocation function; using the wrong form gives undefined behavior even when a trivial element type appears to work. Prefer a value, std::vector for dynamic sequences, std::array for fixed-size value storage, or std::unique_ptr<T[]> when a dynamically allocated array is specifically required. These owners retain the correct destruction policy across exceptions and early returns.
A pointer returned by an ordinary new-expression must be passed to the matching single-object delete form, while storage from a new[] expression requires delete[]. The array form destroys the constructed elements and invokes the matching deallocation function; using the wrong form gives undefined behavior even when a trivial element type appears to work. Prefer a value, std::vector for dynamic sequences, std::array for fixed-size value storage, or std::unique_ptr<T[]> when a dynamically allocated array is specifically required. These owners retain the correct destruction policy across exceptions and early returns.
Connect the conclusion directly to the type, lifetime, ownership, overload-resolution rule, or library contract present in the question. State implementation dependence or a violated precondition explicitly instead of merely guessing output.