4

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 a std::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?

6
  • and what's wrong with simply std::vector? when shared_* is used it usually means there is no owner, which is frequently a desing defect. Commented Jul 22, 2011 at 22:19
  • I need to be able to transfer ownership. Commented Jul 22, 2011 at 22:20
  • 4
    @Gene Bushuyev: I disagree. At least when I use shared_ptr and friends (or other smart pointer implementations), they are used only when there are multiple simultaneous owners with lifetimes that cannot be determined during compile time. Granted they don't occur often, but they have a legitimate use. Commented Jul 22, 2011 at 22:25
  • 1
    @Catskul: the way to transfer ownership of a vector is swap(). You should only need shared_ptr<vector<T> > when you're actually using the refcounting, that is when multiple different owners need to access the same vector, and you don't know which one will need it longest. Commented Jul 22, 2011 at 22:55
  • 1
    @Gene Bushuyev: In that case, I totally agree. :-) Commented Jul 23, 2011 at 4:20

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Can you think of any particular reason an array would be any faster than std::vector?
I can imagine arrays being marginally faster when you know the number of elements in advance, because new[] may not overallocate (at least, not as much as std::vector). Again, the speed benefits are usually marginal.
std::vector simply wraps an ordinary array, so at least accesses should be as fast as raw arrays when the compiler optimizes the code.
@Larsmans: completely agreed. @Catskul: to enumerate some other rather farfetched cases: optimization turned off, not inlined vector methods invocation (bad compiler?), improper usage of iterators, overuse of std::vector::erase() etc. It's why I said "theoretically it's faster"
3

shared_ptr to std::vector

  • + allows amortized constant time push_back
  • - introduces an extra level of indirection over std::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.

8 Comments

re: shared_ptr to std::vector Won't the extra level of indirection be likely optimized away in most cases as far as performance is concerned?
@Catskul: an extra level of indirection can't be optimized away in nearly any practical situation. Whether the performance hit is significant is another question entirely. I'd say use shared_array when possible, because it's simple, and shared_ptr to vector when you need to change the length of the array.
@larsman either might look like (*imageData)[index]. In each case a dereference of imageData happens and then a second dereference happens after the pointer arithmetic for the subscript operator in either case. It seems like they would be equivalent, but also optimizable at least in the case of the first dereference, no?
@Catskul: shared_array<T> wraps a pointer to T; vector<T> wraps a pointer to T; shared_ptr<vector<T> > wraps a pointer to a pointer to T. With shared_array<T> a, you'd get the i'th element with a[i]; with the shared_ptr solution, you'd get the same with (*a)[i].
@Catskul: If you were a compiler, how would you optimise out a pointer dereference?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.