huff
huff

Reputation: 2024

Boost shared_ptr to array, getting size

Ok I am new to this approach, so it may be wrong. But basically I want to have a shared_ptr to an array. With Boost 1.53+, it seems that there's no need to use make_shared_array. Anyway I don't even want to allocate it- but create it from a pointer and a size, and just let it be managed.

So:

// I have a (char* p) to the array, and a (size_t sz) specifying the length of it

// shall I create a shared_ptr like this...
boost::shared_ptr<char[]> sp(p, sz); // is this even right?

// now, how can I get the size of the structure? (assuming sz is out of scope)

Is it lost? Shall I then wrap everything up in another class that also remembers the size of the stored array?

Upvotes: 0

Views: 1680

Answers (1)

Bill Lynch
Bill Lynch

Reputation: 82006

You can do:

boost::shared_ptr<char[]> sp(new char[100]);

But you'll need to manually keep track of the size (100 in this case).

Upvotes: 1

Related Questions