I have a C library function:
uint8_t* c_func();
This either returns a valid uint8_t
pointer allocated with malloc()
, or NULL
on error. I want to wrap it in a std::unique_ptr()
as follows:
struct FreeDeleter {
void operator()(void *p) const {
std::free(p);
}
};
template <typename T>
using unique_fptr = std::unique_ptr<T, FreeDeleter>;
std::free()
does nothing if the pointer being passed to it is NULL
, so this should work as expected.
Is this design correct, and does it follow the best practices of wrapping a raw pointer in C++?