As far as I know, std::string creates an identical array-copy of its content when you call the c_str()/data() methods (with or without the terminating NUL-char; it does not matter here). Anyway, does the object also take care of freeing this array or do I have to?
In short:
std::string hello("content");
const char* Ptr = hello.c_str();
// Use it....
delete[] Ptr; //// Really ???
I just want to be on the safe side when it comes to memory allocation.
No, you don’t need to deallocate the
ptrpointer.ptrpoints to a nonmodifiable string located somewhere to an internal location (actually, this is an implementation detail of the compilers).Reference:
C++ documentation:
Get C string equivalent
Generates a null-terminated sequence of characters (c-string) with the same content as the string object and returns it as a pointer to an array of characters.
A terminating null character is automatically appended.
The returned array points to an internal location with the required storage space for this sequence of characters plus its terminating null-character, but the values in this array should not be modified in the program and are only guaranteed to remain unchanged until the next call to a non-constant member function of the string object.