开发者

how can i convert boost::array to boost::shared_ptr

boost::array<uint8_t,1000> buffer;
ByteBuffer b((boost::shared_ptr<uint8_t>)buffer.data(), buffer.size());

well, the cast kinda w开发者_JAVA技巧orks but when the debuger go to the end of the method it gives me an error so i was wondering if there aonther way to cast/convert it to make it works!


This makes no sense. You are attempting to "hack" around errors by applying arbitrary casts.

shared_ptr is a wrapper around a pointer to dynamically-allocated objects. It manages their dynamic lifetime.

array is a wrapper around an array with automatic storage duration. The uint8_t array it wraps is not a dynamically-allocated block of memory.

So, this conversion is completely inappropriate and will result in Undefined Behaviour... and that's even if you can get it to compile. In general, use C++ casts like static_cast, which error out on improper conversions like this in a way that C casts do not.

(It's tempting to use C casts so that it "just works", but in reality what you're usually doing is burying your head in the sand and singing "ahhh" while your code crumbles into nonsense.)

Why not just:

ByteBuffer b(buffer.data(), buffer.size());

If ByteBuffer really requires a shared_ptr as input, then you're going to have to either store the data in a shared_ptr-controlled dynamic block of memory to begin with, or copy it into one for this operation.


I'm not sure what you are trying to do. If you want to create a reference counted block of 1000 uint8_t, then create a shared array:

boost::shared_array<uint8_t>(new uint8_t[1000])


The only way to have that work is the following. And I think it is a VERY BAD IDEA:

boost::array<uint8_t,1000> buffer;
ByteBuffer b(boost::shared_ptr<uint8_t>(buffer.data(), [](uint8_t*){}), buffer.size()); // Note that the deleter does nothing.


What is ByteBuffer? Is that a standard library class, a class from elsewhere, or one you wrote yourself? What is the declaration, what type does it expect on instantiation?

It sounds to me like a class for managing a chunk of raw memory -- but boost:array or shared_array ALREADY does that. What do you actually want to acheive?

I think either:

  • ByteBuffer expects a raw pointer (char*) to existing memory, and you mistakenly use shared_ptr (shared_ptr only applies to single an object allocated by 'new')
  • or, You don't need ByteBuffer at all, everything you need can be done by a boost::array
  • or, ByteBuffer expects a shared_ptr to an object of some sort rather than an array
  • or, ByteBuffer really does want a shared_ptr to an array, but this is almost certainly broken. It is possible to do this, but (a) casting doesn't do it, that just says "this memory, interpret it as if it's another sort of object, even if that just puts garbage data in" (unless the two types have an inbuild conversion, which array and shared_ptr don't) and (b) it's weird and fiddly and I honestly don't believe for a second that ByetBuffer expects this, or if it does, that it's worth using.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜