470,631 Members | 2,027 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,631 developers. It's quick & easy.

STL string class

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.

Apr 21 '07 #1
3 1702
mi******@gmail.com 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-- after the string is
destructed you must not access the memory returned by c_str().
Apr 21 '07 #2
"Mark P" <us****@fall2005REMOVE.fastmailCAPS.fmwrote in message
news:cB****************@newssvr17.news.prodigy.net ...
: mi******@gmail.com 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
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <http://www.brainbench.com

Apr 21 '07 #3
On Apr 21, 4:57 am, "Ivan Vecerina"
<_INVALID_use_webfo...@ivan.vecerina.comwrote:
"Mark P" <use...@fall2005REMOVE.fastmailCAPS.fmwrote in message
news:cB****************@newssvr17.news.prodigy.net ...: mike7...@gmail.comwrote:
: When you use she STL string class and the c_str() function, how does
: the memory returned by c_str() get allocated and destroyed?
: 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
Any operation which permits modification, in fact. Calling [],
at(), begin() or end() on a non-const string, or through a
non-const reference to the string, may also invalidate the
pointer.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 21 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Derek Hart | last post: by
23 posts views Thread by YinTat | last post: by
4 posts views Thread by Carl Youngblood | last post: by
19 posts views Thread by Mike Tyka | last post: by
27 posts views Thread by djake | last post: by
6 posts views Thread by Sanjay Pais | last post: by
9 posts views Thread by jerry.upstatenyguy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.