Connecting Tech Pros Worldwide Help | Site Map

STL string class

 
LinkBack Thread Tools Search this Thread
  #1  
Old April 21st, 2007, 12:05 AM
mike7411@gmail.com
Guest
 
Posts: n/a
Default 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.


  #2  
Old April 21st, 2007, 12:15 AM
Mark P
Guest
 
Posts: n/a
Default Re: STL string class

mike7411@gmail.com wrote:
Quote:
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().
  #3  
Old April 21st, 2007, 03:05 AM
Ivan Vecerina
Guest
 
Posts: n/a
Default Re: STL string class

"Mark P" <usenet@fall2005REMOVE.fastmailCAPS.fmwrote in message
news:cBcWh.1109$ns5.542@newssvr17.news.prodigy.net ...
: mike7411@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

  #4  
Old April 21st, 2007, 09:45 AM
James Kanze
Guest
 
Posts: n/a
Default Re: STL string class

On Apr 21, 4:57 am, "Ivan Vecerina"
<_INVALID_use_webfo...@ivan.vecerina.comwrote:
Quote:
"Mark P" <use...@fall2005REMOVE.fastmailCAPS.fmwrote in message
Quote:
news:cBcWh.1109$ns5.542@newssvr17.news.prodigy.net ...: mike7...@gmail.comwrote:
Quote:
: When you use she STL string class and the c_str() function, how does
: the memory returned by c_str() get allocated and destroyed?
Quote:
: The memory is controlled by the string object
Yep.
Quote:
: after the string is destructed
: you must not access the memory returned by c_str().
Quote:
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: james.kanze@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

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.