Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > STL string class

Reply
Thread Tools

STL string class

 
 
mike7411@gmail.com
Guest
Posts: n/a
 
      04-20-2007
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.

 
Reply With Quote
 
 
 
 
Mark P
Guest
Posts: n/a
 
      04-21-2007
http://www.velocityreviews.com/forums/(E-Mail 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-- after the string is
destructed you must not access the memory returned by c_str().
 
Reply With Quote
 
 
 
 
Ivan Vecerina
Guest
Posts: n/a
 
      04-21-2007
"Mark P" <(E-Mail Removed)> wrote in message
news:cBcWh.1109$(E-Mail Removed) ...
: (E-Mail 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
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      04-21-2007
On Apr 21, 4:57 am, "Ivan Vecerina"
<(E-Mail Removed)> wrote:
> "Mark P" <(E-Mail Removed)> wrote in message


> news:cBcWh.1109$(E-Mail Removed) ...: (E-Mail 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?


> : 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: (E-Mail Removed)
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
stl questions: how can I compare 2 stl list? silverburgh.meryl@gmail.com C++ 5 04-16-2006 09:57 PM
a stl map which use stl pair as the key Allerdyce.John@gmail.com C++ 2 02-22-2006 07:25 AM
Copy elements from one STL container to another STL container Marko.Cain.23@gmail.com C++ 4 02-16-2006 05:03 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
To STL or not to STL Allan Bruce C++ 41 10-17-2003 08:21 PM



Advertisments