Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Resetting unique_ptr to an array of characters

I’m porting an old C++ program to modern C++. The legacy program uses new and delete for dynamic memory allocation. I replaced new with std::unique_ptr, but I’m getting compilation error when I try to reset the unique_ptr.

Here is the striped down version of the program. My aim is to get rid of all the naked new.

#include <memory>

enum class Types {
    ONE,
    TWO,
};

// based on type get buffer length
int get_buffer_len(Types type) {
    if(type == Types::ONE) return 10;
    else if(type == Types::TWO) return 20;
    else return 0;
}

int main() {
    Types type = Types::ONE;
    std::unique_ptr<char[]> msg{};

    auto len = get_buffer_len(type);
    if(len > 0) {
        msg.reset(std::make_unique<char[]>(len));
    }

    // based on type get the actual message
    if(type == Types::ONE) {
        get_message(msg.get());
    }
}

I get the following compilation error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

error: no matching function for call to 'std::unique_ptr<char []>::reset(std::__detail::__unique_ptr_array_t<char []>)'
      |         msg.reset(std::make_unique<char[]>(len));
      |         ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

>Solution :

If you look at the reset function, it takes a ptr to memory, not another unique ptr:

// members of the specialization unique_ptr<T[]>
template< class U >
void reset( U ptr ) noexcept;

This function is designed to allow you to reset a unique pointer and simultaneously capture memory that you intend to manage with said unique_ptr. What you are looking to do is assign an r-value unique_ptr to ann existing unique_ptr (msg), for which c++ also has an answer:

unique_ptr& operator=( unique_ptr&& r ) noexcept;

Move assignment operator. Transfers ownership from r to *this as if by calling reset(r.release()) followed by an assignment of get_deleter() from std::forward(r.get_deleter()).

So you can instead just do:

msg = std::make_unique<char[]>(len);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading