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:
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));
| ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CodePudding user response:
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);