473,403 Members | 2,284 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

std::string substr() question

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
3 3394
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Giampiero Gabbiani | last post by:
Hi to all, Is there a simple way to implement a strip algorithm on std::string using STL? I'm sure that it's possible to implement it using some transform, but my knowledge of STL is POOR. ...
13
by: Dave | last post by:
Can anybody tell me why the code below should produce the error below? I'm going nuts here! Am I missing something incredibly obvious and simple? std::string::size_type idx; std::string...
11
by: Christopher Benson-Manica | last post by:
Let's say I have a std::string, and I want to replace all the ',' characters with " or ", i.e. "A,B,C" -> "A or B or C". Is the following the best way to do it? int idx; while(...
1
by: Chris Mantoulidis | last post by:
PROBLEM: I'm having some weird problems with string::find() (in ParamGenerate()), but since I'm not sure if that is the source of all bad output in my program, I'm posting least code that's...
1
by: Tero Toivanen | last post by:
Dear experts, I am doing code to Solaris 9 system with C++. I get every now and then segmentation fault in the following code that removes heading and trailing white spaces (mLineStr is of...
32
by: Wolfgang Draxinger | last post by:
I understand that it is perfectly possible to store UTF-8 strings in a std::string, however doing so can cause some implicaions. E.g. you can't count the amount of characters by length() | size()....
2
by: Jarek Bednarz | last post by:
Hi all, Following code causes strAddress to contain garbage data. When I decrease number of characters that appear before '\\' then everything is ok. 12 chars before '\\' seems to be a magic...
7
by: Marcus Kwok | last post by:
std::string::npos is described in _TC++PL:SE_ (Section 20.3.4) as the "all characters" marker. I tried to use it this way, but my program crashes: #include <iostream> #include <string> int...
2
by: FBergemann | last post by:
if i compile following sample: #include <iostream> #include <string> int main(int argc, char **argv) { std::string test = "hallo9811111z"; std::string::size_type ret;
11
by: Christopher Pisz | last post by:
Is std::string::npos always going to be less than any std::string 's size()? I am trying to handle a replacement of all occurances of a substr, in which the replacement also contains the substr....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.