I have to use a library function that allocates a bit of memory for a generated string and returns a char*, with the expectation that the caller eventually free the memory with free().
// Example declaration of the library function: char* foo(); // ... // Example usage: auto foo_str = foo(); // ... free(foo_str); Is it possible to construct a std::string from this pointer, passing ownership of the memory to the string object so it will be freed when the string is destructed? I know I could implement my own wrapper that would give this RAII behavior, but I'm guessing this wheel has already been invented once.
std::unique_ptrfrom your raw pointer? Or is C++11 not an option?unique_ptrdoes not usefree(). It will use some form of operator delete.unique_ptrcan delete however you want it to delete.unique_ptr! I failed by not considering that. Of course C++11 is an option, I tagged the question with it :)