Search code examples
c++pointersboostcharshared-ptr

Can we keep char[undetermined_during_compile_time_size] inside of boost::shared_ptr?


I could have a queue<char*> file_queue; but than I would need to clean up after each char*. I have a dinamic variable int buff_length; that would be length of each char in file_queue. It would be set once from a config file before queue creation. So I wonder - is it possible to keep char[buff_length] inside one boost::shared_ptr and how to do such thing?


Solution

  • You can use a shared_array:

    boost::shared_array<char> ptr(new char[buff_length]);
    

    Any reason why std::string or std::vector<char> is not appropriate for your situation?


    Note, with C++11 you can use the following:

    std::unique_ptr<char[]> ptr(new char[buff_length]);
    

    std::unique_ptr has a specialization to deal with arrays. I was under the mistaken belief that boost::shared_ptr did too, but evidently that was incorrect.