Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should smart pointers be used to hold arrays?

Tags:

c++

c++11

I've see many answers that suggest using smart pointers to hold dynamically-allocated arrays in memory. My view has always been that if the size is known it should be wrapped in an std::array, and likewise if the size wasn't known (i.e. by making the pointer type of the smart pointer an array of unknown bound and reset()'ing it later on) one should use std::vector. And indeed that is what I always do.

For example, recently I saw an answer that used std::unique_ptr<int[5]> p(new int[5]). This seems analogous to constructing an std::array<int, 5> since the size is known. Moreover, the added benefits are that std::array statically-allocates its array and contains array-like features like the size, iterators, and more.

So what are the reasons for using a smart pointer to hold an array over using other standard containers made specifically for that purpose?

like image 658
David G Avatar asked Sep 06 '14 14:09

David G


People also ask

When should smart pointers be used?

Use when you want to assign one raw pointer to multiple owners, for example, when you return a copy of a pointer from a container but want to keep the original. The raw pointer is not deleted until all shared_ptr owners have gone out of scope or have otherwise given up ownership.

Why should I consider using smart pointers?

Smart pointers try to prevent memory leaks by making the resource deallocation automatic: when the pointer to an object (or the last in a series of pointers) is destroyed, for example because it goes out of scope, the pointed object is destroyed too.

What is a smart pointer why it is used and describe the types?

Smart pointers are just classes that wrap the raw pointer and overload the -> and * operators; this allows them to offer the same syntax as a raw pointer. C++11 has three types of smart pointers that are defined in the <memory> header of the Standard Library. They are: std::unique_ptr. std::shared_ptr.


2 Answers

Firstly, unique_ptr<T[]> does not require a static size- it can be just unique_ptr<int[]>.

Moreover, the added benefits are that std::array statically-allocates its array

This is not strictly guaranteed to be a benefit. Consider if I have a 10-megabyte array- that's going to blow my stack.

Generally speaking, people choose this approach when they want the array size to be fixed on creation, but be able to mutate the members. Note that for std::vector, you can only make both the elements and the vector const, or neither. You cannot make only the vector but not the elements const.

like image 132
Puppy Avatar answered Nov 09 '22 12:11

Puppy


As you said, std::array is a static array. If you declare a std::array<int, 5> on the stack, the actual array is on the stack. Whereas std::unique_ptr<int[]>(new int[5]) puts the actual array on the heap instead. That might not seem like a big difference for small arrays, but it is a big deal for large arrays. Just because an array is fixed length does not mean it should always be on the stack. It depends on the size of the array and how much stack space is available. Putting a large array on the stack risks throwing a stack overflow error. Putting a large array on the heap will not throw that (unless the stack is already full so the unique_ptr cannot be constructed after the array is allocated), though it does have a lesser risk of throwing an out of memory error if the heap/memmgr is full, but that would happen before the Unique_ptr is constructed.

like image 39
Remy Lebeau Avatar answered Nov 09 '22 10:11

Remy Lebeau