4

I am trying to declare a dynamic int array like below:

int n;
int *pInt = new int[n];

Can I do this with std::auto_ptr?

I tried something like:

std::auto_ptr<int> pInt(new int[n]);

But it doesn't compile.

I'm wondering if I could declare a dynamic array with auto_ptr construct and how. Thanks!

Azeem
  • 11,148
  • 4
  • 27
  • 40
itnovice
  • 503
  • 1
  • 4
  • 20

2 Answers2

8

No, you cannot, and it will not: C++98 is very limited when it comes to arrays, and auto_ptr is a very awkward beast that often doesn't do what you need.

You can:

  • use std::vector<int>/std::deque<int>, or std::array<int, 10>, or

  • use C++11 and std::unique_ptr<int[]> p(new int[15]), or

  • use C++11 and std::vector<std::unique_ptr<int>> (though that feels too complicated for int).

If the size of the array is known at compile time, use one of the static containers (array or an array-unique-pointer). If you have to modify the size at runtime, basically use vector, but for larger classes you can also use a vector of unique-pointers.

std::unique_ptr is what std::auto_ptr wanted to be but couldn't due to the limitations of the language.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • Sorry for this dumb question, but `std::unique_ptr` can handle arrays as well as single objects properly then, right? – Seth Carnegie Nov 30 '11 at 02:10
  • 2
    @SethCarnegie: Yes, it can, and it even provides `[]`-access for arrays. (And it prohibits upgrading to `shared_ptr`.) – Kerrek SB Nov 30 '11 at 02:13
  • @Kerrek: Thanks. Is the array length considered known at compile time in the following code: `void foo(int n) {int *pInt = new int[n];} int main() { foo(10); }` Thanks. – itnovice Nov 30 '11 at 02:18
  • @itnovice: no, it isn't. C++ doesn't have a sufficiently sophisticated notion of a "constant expression". What you would have to do is `template void foo() { int a[n]; }; int main() { foo<10>(); }` – Kerrek SB Nov 30 '11 at 02:19
  • `std::unique_ptr` can be used for variable sizes. `int i = 10; std::unique_ptr p(new int[i]);` would be OK. – fefe Nov 30 '11 at 02:28
  • @itnovice: Sorry for my mental block; thanks to fefe's comment, what you *can* do is `void foo(int n) { std::unique_ptr p(new int[n]); }`. That's basically the safe, dynamic version of the code in your comment. – Kerrek SB Nov 30 '11 at 02:38
0

You cannot. std::auto_ptr cannot handle dynamic arrays, as the reallocation is different (delete vs delete[]).

But I wonder what the compilation error may be ...

fefe
  • 3,342
  • 2
  • 23
  • 45
  • Thanks. The compiler errors look like `temp.cpp:19: error: no match for ‘operator[]’ in ‘pInt[0]’ temp.cpp:21: error: no match for ‘operator[]’ in ‘pInt[i]’ temp.cpp:21: error: no match for ‘operator[]’ in ‘pInt[(i - 1)]’` – itnovice Nov 30 '11 at 02:10
  • @itnovice yeah, because you're trying to use (something that thinks it is) a pointer to a single object as an array. As has been said, `auto_ptr` is for single objects so it doesn't have `operator[]`. – Seth Carnegie Nov 30 '11 at 02:14