6

Now I'm rewriting a part of my code to use C++11 standard. In some places I found the following code:

boost::shared_array<uint8_t> array;

Does it make to replace it with:

std::shared_ptr<std::vector<uint8_t>> array;

I'm trying to replace all boost's functionality that already presented in C++11 in my code.

I need to clarify a bit. Actually I need a raw array (but with refcount, so it can be automatically deleted), no need for all those vector features. So boost::shared_array solves the problem I want without any additional cost. But I'm trying to make my code uses new standard as much as possible (though many libraries from boost still not covered by the new standard).

Thanks.

maverik
  • 5,508
  • 3
  • 35
  • 55
  • Well, you could get rid of a boost dependency in that case. For the rest, it's functionally equivalent at a high level. – Germán Diago May 13 '13 at 15:59

1 Answers1

4

Given the current state of affairs in C++11 support in compilers and the laziness of people maintaing builds, I would leave that as-is now. Boost has the nice property of working virtually everywhere, including old compilers, and the change you want to make will hardly improve the code.

This code also isn't exactly the same.

Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135
  • Thanks. Was it _that_ hard to add shared_array and/or scoped_array to the standard? Just wondering. – maverik May 13 '13 at 11:26
  • 4
    @maverik: C++11 has `shared_ptr` and `unique_ptr` specializations, which I guess fullfill exactly that purpose. – rubenvb May 13 '13 at 11:32
  • @rubenvb, nice observation. – maverik May 13 '13 at 11:35
  • 3
    @rubenvb: C++11 does not have a `shared_ptr` specialization, you have to provide your own custom deleter to make `shared_ptr` work with arrays. – Andy Prowl May 13 '13 at 11:43
  • @rubenvb should have been an answer instead, since as far as I see, shared_ptr can be used instead of boost::shared_array – BЈовић May 13 '13 at 11:44
  • 2
    @AndyProwl Wait, what? That's absolutely not how I remember it. Valgrind would have told me differently if it were true – sehe May 13 '13 at 11:45
  • 1
    Andy is right here, you have to supply proper deleter to `shared_ptr` for it to work correctly. If it worked for you @sehe, that might be just your standard library playing nice, but the standard doesn't have it. – Bartek Banachewicz May 13 '13 at 11:45
  • @AndyProwl oh wow. That is surprising. But indeed a custom deleter would solve the "oh crap, memory leak". Any idea as to why this is not the case though? – rubenvb May 13 '13 at 11:47
  • okay, it seems I relied a bit too much on valgrind there: http://stackoverflow.com/q/8947579/85371 (I hate to see new pitfalls slipping through the cracks in C++11). – sehe May 13 '13 at 11:48
  • @rubenvb: You mean, why there is no specialization of `shared_ptr` in C++11? I guess nobody just cared too much to formulate a proposal. I *think* there is one for C++14, though – Andy Prowl May 13 '13 at 11:48
  • @rubenvb Implementation of the standard library you use. See also this : http://stackoverflow.com/questions/10319009/unique-ptrt-lambda-custom-deleter-for-array-specialization – BЈовић May 13 '13 at 11:49
  • @AndyProwl **Ah**: And it **_does work_** as expected for `std::unique_ptr`. Sigh. There was my confusion. Valgrind was right after all :). Also, see [std::default_delete<>](http://en.cppreference.com/w/cpp/memory/default_delete) – sehe May 13 '13 at 11:50
  • 1
    @sehe: Indeed, the specialization for `std::unique_ptr<>` is defined in the Standard. Btw [here](http://open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3640.html) is the proposal to have `shared_ptr` as well – Andy Prowl May 13 '13 at 11:52
  • 1
    @maverik `unique_ptr` *will* call `delete[]`, so no memory leaks will happen. You can only access the data by raw get, though (basically all restrictions of `unique_ptr` still hold) – Bartek Banachewicz May 13 '13 at 12:01