As nv3 said, don't do it - the compiler could generate code to do just about anything.
Anyway, rather than worrying about using
delete
or
delete[]
why not take the easy, incredibly safe, way out and use
std::unique_ptr
,
std::shared_ptr
or
std::vector
to manage memory for you?
So if you want an array of things that have a default constructor you can use:
std::vector<my_class>( 20 );
to create a
vector
of 20 objects of class
my_class
. And you won't have to worry about whether you need to call
delete
or
delete[]
. And you can do just about anything you did with a built in array - including indexing past the end if you feel so careless.
So Ash's advice for pain free memory leak detection in C++ is:
- don't manually manage memory
- use stack based/automatic objects wherever you can
- where you can't use them use
std::vector
,
std::unique_ptr
or
std::shared_ptr
instead
Do that lot and you can spend the rest of your programming life with C++ not leaking any memory, never double deleting objects and having all the other "fun" you can have with pointers.