std::shared_ptr<T[]> 与 std::array<T, size>/std::vector<T>

huangapple go评论62阅读模式
英文:

std::shared_ptr<T[]> VS std::array<T, size>/std::vector<T>

问题

在C++中,shared pointer具有一个可以包含原始数组的构造函数(例如:std::shared_ptr<int[]>)。

与std::array或std::vector相比,使用它的好处是什么?

尝试搜索,询问gpt。至于std::vector,有一个建议可以通过std::shared_ptr来避免内存分配,但这并不解释为什么在存在std::array时要使用它。

英文:

So, I have a question, the answer to which I could not find. In C++ shared pointer has a constructor that can contain a raw array(std::shared_ptr<int[]>,for example.)

What are the benefits of using it compared to std::array or std::vector?

Tried to search, ask gpt. As for std::vector, there is a suggestion that memory allocation can be avoided with std::shared_ptr, but that doesn't explain why using this when there is a std::array

答案1

得分: 5

与所有其他列出的选项不同,std::array要求在编译时已知大小,并且不存储元素在堆上(除非它本身位于堆上)。

std::vector<T>可以处理在编译时未知大小的情况,如果是这种情况,应该是您的默认选择。它始终将元素存储在堆上。

std::unique_ptr<T[]>可以被看作是一个精简版的std::vector。它不知道自己的大小,这可以节省一小部分内存(sizeof(std::unique_ptr<T[]>)通常是sizeof(void *),而sizeof(std::vector<T>)通常是3 * sizeof(void *))。

由于它不存储自己的大小,它的默认功能非常有限:它不能被复制(只能移动),不能插入或移除元素等等。如果您单独存储大小,那么它就变成了带有额外步骤的std::vector

std::shared_ptr<T[]>std::unique_ptr<T[]>添加了一个额外的功能 - 共享所有权,就像任何其他std::shared_ptr<???>一样。

英文:

Unlike all other listed options, std::array requires the size to be known at compile-time, and doesn't store the elements on the heap (unless it itself is on the heap).

std::vector&lt;T&gt; can work with a size not known at compile-time, and should be your default choice in that case. It always stores the elements on the heap.

std::unique_ptr&lt;T[]&gt; can be thought of as a stripped down std::vector. It doesn't know its size, which saves a tiny bit of memory (sizeof(std::unique_ptr&lt;T[]&gt;) is normally sizeof(void *), while sizeof(std::vector&lt;T&gt;) is normally 3 * sizeof(void *)).

Because it doesn't store its size, there's very few things it can do out of the box: it can't be copied (only moved), it can't insert or remove elements, etc. You can do all of this manually if you store the size separately, but then it's just std::vector with extra steps.

std::shared_ptr&lt;T[]&gt; adds an extra feature to std::unique_ptr&lt;T[]&gt; - shared ownership, like any other std::shared_ptr&lt;??&gt;.

答案2

得分: 1

使用容器和智能指针取决于用法和设计:

std::vector在堆上创建对象的动态数组。
std::array在栈上创建对象的静态数组,大小在编译时已知。
std::shared_ptr<int[]>保持资源的引用计数,这里资源是指向数组的指针。
如果使用范围有限且大小在编译时已知,则可以在栈上使用std::array。

当您希望在函数、容器、线程(多线程安全对象或const)之间共享资源的所有权时,shared_ptr是合适的。但使用shared_ptr并不意味着我们可以避免分配,您仍然需要传递一个由它管理的资源指针。

至于vector,它在堆上分配数据,但需要注意vector本身不应该超出作用域,否则析构函数会释放资源,因为它不像shared_pointer那样具有引用计数。

如果使用vector,至少会进行一次分配。

如果使用array,它在栈上,因此不会进行分配。

shared_ptr接受一个指针,该指针必须在某处分配。其他分配是控制块的分配。因此,直到使用std::make_shared为止,可以优化分配的次数。

英文:

Use of containers and smart pointers depends on usage and design:

std::vector creates a dynamic array of objects on heap
std::array creates a static array of object on stack, size known at compile time
std::shared_ptr&lt;int[]&gt; keeps the reference count of the resource, here resource is a pointer to an array

std::array is on the stack, if usage is limited to a scope and size is known at compile time then array is fine.

when you want to share the ownership of the resource across functions, containers, thread(multi-thread safe object or const), shared_ptr is suitable. But using shared_ptr doesnt mean we can avoid allocation, you need to pass a resource pointer which it manages.

As for vector, it allocates data on heap but need to take care that vector itself should not go out of scope otherwise destructor will free up the resource as it doesnt have refrence count like shared_pointer.

If you use vector ,atleast one allocation is done.

If you use array, its on stack so no allocation is done.

shared_ptr takes a pointer, which has to be allocated somewhere. other allocation is of control block. thus 2 allocation until std::make_shared is used which can optimize the number of allocations.

huangapple
  • 本文由 发表于 2023年6月16日 02:38:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76484605.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定