: (e-mail address removed) wrote:
: > When you use she STL string class and the c_str() function, how does
: > the memory returned by c_str() get allocated and destroyed?
: >
: > Thank you.
: >
:
: The memory is controlled by the string object
Yep.
: after the string is destructed
: you must not access the memory returned by c_str().
Not only destruction, but any operation that modifies
the string may invalidate the memory that was returned
by c_str().
I.e.:
std::string s = "Hello";
char const* p = s.c_str();
std::cout << p << std::endl; //ok
s.append('.');
std::cout << p << std::endl; // UNDEFINED BEHAVIOR
hth -Ivan