为什么要使用其中之一:`boost::shared_array` VS `boost::shared_ptr`?
因此,要处理图像或类似图像的大块内存,显然有很多选择。
因为我是智能指针和 RAII 的粉丝,所以我想知道使用 :
- a
shared_ptr
到std::vector
或
- 使用 a 是否更聪明。
shared_array
指向动态分配的数组。
选择其中之一对概念、实践和性能有何影响?
So to deal with large blobs of memory either for an image or similar there are clearly lots of options.
Since I'm a fan of smart pointers and RAII I'm wondering about whether it's smarter to go with :
- a
shared_ptr
to astd::vector
or
- to go with a
shared_array
pointing to a dynamically allocated array.
What are the conceptual, practical, and performance implications of choosing one vs the other?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这与比较 std::vector 与 C 数组相同。
将
shared_array
视为 RAII C 数组。你得到的只是自动内存释放。在处理返回数组的第三方代码时很有用。理论上,在某些边缘情况下它比 std::vector 更快,但灵活性和安全性较差。
std::vector 可能是更好的选择。
It's the same as comparing std::vector vs. C array.
Think about
shared_array
as a RAII C array. What you get is just automatic memory deallocation. Useful in cases when you deal with 3rd-party code that returns arrays.Theoretically it's faster than std::vector in some edge cases, but much less flexible and less secure.
std::vector is probably the better choice.
shared_ptr
到std::vector
+
允许分摊常数时间push_back
-
引入了一个std::vector
上的额外间接层shared_array
+
不会引入额外的间接层-
不会允许摊销常数时间追加,除非你自己实现它,这又需要额外的间接级别。shared_ptr
tostd::vector
+
allows amortized constant timepush_back
-
introduces an extra level of indirection overstd::vector
shared_array
+
does not introduce an extra level of indirection-
does not allow amortized constant time append, unless you implement it yourself, which again would take an extra level of indirection.