samedi 28 novembre 2015

c++11: unique_ptr and new[]

If I'm right, unique_ptr is a kind of smart pointer, meaning that we don't need to delete it manually.
However, if we call the function release, it may leak memories.

For example:

unique_ptr<int> up(new int());
up.release();

Now we get memory leak because release wont' call delete.

However, here is something I can't understand:

unique_ptr<int[]> up(new int[10]);
up.release(); // call delete[] automatically

Why delete[] can be called automatically but delete can't be?

Aucun commentaire:

Enregistrer un commentaire