开发者

reallocating boost::shared_array

I have a shared_array: boost::shared_array myarr(new char[m_length]);

I would like to reallocate the array. I thought of creating a new shared_array with the wanted size and using the swap boost method but this will copy the referance count as well. Do you have another idea?

//new_length>m_length
void f开发者_开发知识库unc(boost::shared_array<char> &myarr,int new_length)
{
       boost::shared_array<char> new_arr(new char[new_length]);
       myarr.swap(new_arr);
}


Why not just instead use a boost::shared_ptr<std::vector<char> >? Let the standard library handle resizing.

(In fact, depending on why you were using shared_array in the first place, you might well get away with just using a std::vector, and passing it around by reference carefully.)


boost::shared_array::reset should do the trick

myarr.reset(new char[new_length]);

boost::shared_array::reset deletes the old allocated array, swapping it with the newly allocated one.

Edit: Ignore this answer, it doesn't solve his problem

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜