cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
Beginners
Deallocating after c_str()?
Deallocating after c_str()?
Feb 10, 2012 at 10:45pm UTC
santih87
(12)
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 UTC
moorecm
(1932)
You might want to try compiling that...
Feb 10, 2012 at 10:58pm UTC
santih87
(12)
I just did.... it compiles
Feb 10, 2012 at 11:00pm UTC
Peter87
(11234)
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 UTC
santih87
(12)
Gotcha!, Thanks Peter!
Topic archived. No new replies allowed.