Returning ‘c_str’ from a function

strdup allocates a copy of the string on the heap, which you have to free manually later (with free() I think). If you have the option, it would be much better to return std::string.

The static storage of out doesn’t help, because .str() returns a temporary std::string, which is destroyed when the function exits.

Leave a Comment