Search code examples
c++boostrealloc

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 func(boost::shared_array<char> &myarr,int new_length)
{
       boost::shared_array<char> new_arr(new char[new_length]);
       myarr.swap(new_arr);
}

Solution

  • 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.)