Deallocating after c_str()?

Feb 10, 2012 at 10:45pm
Do I have to worry about deallocating the memory used by a string gotten by calling the c_str method from the string class?

for example,

1- string str("hey");
2- const char* cstr = str.c_str();
3- cstr = "abcd";

... after line #3 has been executed, what happened to the memory it was pointing to before?
Feb 10, 2012 at 10:51pm
You might want to try compiling that...
Feb 10, 2012 at 10:58pm
I just did.... it compiles
Feb 10, 2012 at 11:00pm
c_str() just returns pointer to the string's internal buffer so no need to clean up. Just be careful if you manipulate the string, pointers received from earlier calls to c_str() could be invalidated.
Feb 10, 2012 at 11:02pm
Gotcha!, Thanks Peter!
Topic archived. No new replies allowed.