61

How do smart pointers handle arrays? For example,

void function(void)
{
    std::unique_ptr<int> my_array(new int[5]);
}

When my_array goes out of scope and gets destructed, does the entire integer array get re-claimed? Is only the first element of the array reclaimed? Or is there something else going on (such as undefined behavior)?

Luc Danton
  • 34,649
  • 6
  • 70
  • 114
helloworld922
  • 10,801
  • 5
  • 48
  • 85

2 Answers2

98

It will call delete[] and hence the entire array will be reclaimed but I believe you need to indicate that you are using an array form of unique_ptrby:

std::unique_ptr<int[]> my_array(new int[5]);

This is called as Partial Specialization of the unique_ptr.

Bence Kaulics
  • 7,066
  • 7
  • 33
  • 63
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • 36
    Trivia: This is where this syntax got invented: http://groups.google.com/group/comp.lang.c++/browse_thread/thread/469fcc136b8f2d42/59c0491245293db7?q=%3Cchar%5B%5D%3E+group:comp.lang.c%2B%2B+author:hinnant – Howard Hinnant Jul 16 '11 at 00:55
  • 7
    @Howard Hinnant: Thanks for the trivia, I did'nt knew about it. You are the one who invented it, way back in 2001! That is so cool to have you around in here :) – Alok Save Jul 16 '11 at 04:54
  • That's also the only method which makes any sense, consider `typedef int (three_ints)[3]; template void function(void) { unique_ptr p(new T); } function();` – Ben Voigt Jul 16 '11 at 14:42
  • @Als: I get credit for getting it standardized. Luca (no, I don't know who Luca is) gets credit for inventing it, just by asking the right question. :-) – Howard Hinnant Jul 16 '11 at 15:15
  • Are you certain `std::~unique_ptr()` actually would call `delete []` rather than `delete`? I am not really sure... – Albus Dumbledore Nov 13 '13 at 14:32
  • 1
    @AlbusDumbledore: The C++ standard says so and it is the ultimate authority on C++ so my answer is yes, I am certain. – Alok Save Nov 13 '13 at 15:07
  • It's a shame this technique can't be used to declare arrays of pointer to abstract base class. or perhaps I am confusing an array of objects with an array of pointers..? – saxbophone Oct 25 '22 at 14:05
6

Edit: This answer was wrong, as explained by the comments below. Here's what I originally said:

I don't think std::unique_ptr knows to call delete[]. It effectively has an int* as a member -- when you delete an int* it's going to delete the entire array, so in this case you're fine.

The only purpose of the delete[] as opposed to a normal delete is that it calls the destructors of each element in the array. For primitive types it doesn't matter.

I'm leaving it here because I learned something -- hope others will too.

Nathan Monteleone
  • 5,430
  • 29
  • 43
  • 1
    If you use `new[]` you should use `delete[]` and `std::unique_ptr` is smart enough to handle this as well as many other improvements which is why it will replace `std::auto_ptr` soon. – AJG85 Jul 15 '11 at 22:17
  • 4
    -1 : `delete` must always and only be used with `new`; similarly `delete[]` must always and only be used with `new[]`. See C++2003, section 5.3.5 (expr.delete), para 2. – Robᵩ Jul 15 '11 at 22:24
  • 3
    @Nathan: It does matter. Its not just the destructor. The memory allocation routines depend on you deleting the memory with the matching allocation method. Thus new/delete and new[]/delete[] are matched pairs. – Martin York Jul 16 '11 at 04:24