Search code examples
c++memory-managementstdstringconst-char

Proper memory cleanup with c_str() and const char *


I have a function that takes a (char* const*). The data I am provided is in a std::string. So I'm doing this:

void blahblah(std::string str)
{
   const char * cc = str.c_str();
   ConstCharFunction(&cc);
}

This works well. My question is do I need to clean-up the memory used by the const char ala:

delete cc;

Or is cc just a pointer to a mem location in std:string...


Solution

  • It's a pointer to memory allocated in std::string. When the std::string falls out of scope, the memory is released. Be sure not to hold onto the pointer after that!