By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,474 Members | 3,141 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,474 IT Pros & Developers. It's quick & easy.

std::string substr() question

P: n/a
How persistant is the string created by substr()? Is it deleted
immediately the context exits or is it stored internally by the parent
string? I ask because I'm wondering if its safe to keep a copy of the
pointer returned doing mystr.substr(...).c_str() for a short while to
do some processing on it. Obviously if the sub string is deleted it'll
become invalid.

Thanks for any help

B2003
Jun 27 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Boltar wrote:
How persistant is the string created by substr()? Is it deleted
immediately the context exits or is it stored internally by the parent
string? I ask because I'm wondering if its safe to keep a copy of the
pointer returned doing mystr.substr(...).c_str() for a short while to
do some processing on it. Obviously if the sub string is deleted it'll
become invalid.
Like any temporary, the string returned by 'substr' lives as long as
the Standard defines. If no reference to const is bound to it, the
temporary lives till the end of the full expression that caused its
creation. If it's used to initialise an object, it lives until the
object is fully initialised. If a reference to const is bound to it,
it lives as long as the reference. The pointer you obtain using the
'c_str()' member function will be valid as long as the temporary is
alive (and unchanged).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #2

P: n/a
Boltar <bo********@yahoo.co.ukwrote in news:af321295-9a52-4276-bb7e-
a6**********@59g2000hsb.googlegroups.com:
How persistant is the string created by substr()? Is it deleted
immediately the context exits or is it stored internally by the parent
string? I ask because I'm wondering if its safe to keep a copy of the
pointer returned doing mystr.substr(...).c_str() for a short while to
do some processing on it. Obviously if the sub string is deleted it'll
become invalid.
Instead of worrying about it, I encourage you to look at the various
constructors for string and the assign method as well. Both of them allow
the same operations as substr and you don't have to worry about any
lifetime issues you might with temporaries. These have become second
nature to me these days because it directly specifies what I want to do and
doesn't require the compiler to be hyper-intelligent to avoid extra copies.

joe
Jun 27 '08 #3

P: n/a
On Apr 16, 2:18 pm, Joe Greer <jgr...@doubletake.comwrote:
Instead of worrying about it, I encourage you to look at the various
constructors for string and the assign method as well. Both of them allow
the same operations as substr and you don't have to worry about any
lifetime issues you might with temporaries. These have become second
nature to me these days because it directly specifies what I want to do and
doesn't require the compiler to be hyper-intelligent to avoid extra copies.
Sounds like a good idea, I'll give that a try.

B2003
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.