They're all wrong. Do not ever call delete
in a C++ program (unless you are directly implementing a memory-managing primitive, like smart pointer)- always use a smart pointer to handle resource destruction. For arrays, use a container like std::vector<T>
. For strings, there is a dedicated std::string
class. Finally, there are no primitive situations except new
that create objects suitable for delete
, whether directly or correctly (through a smart pointer), so if you did not use new
or call a function that explicitly returns such (which should really return a smart pointer...), then you're definitely doing it wrong.
- Wrong because there's no dynamic memory to delete.
- Wrong because you should use smart pointer (
unique_ptr
looks fine here).
- Same as 1.
- Wrong because you need
std::vector
for dynamically allocated arrays.
- Same as 4.