473,399 Members | 2,159 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,399 software developers and data experts.

is assigning a substr to the parent str OK?

Is the assignment of a substr of the string 'a' to 'a' in the
code below OK?

#include <string>
#include <iostream>

int main(void)
{
std::string a = "hello";
std::cout << a << std::endl;

a = a.substr(0,2);
std::cout << a << std::endl;

return 0;
}

I saw no restrictions on operator= in my STL reference (Josuttis).
But this makes me slightly nervous. I've programmed more in C
than C++, and this feels like it could be rather like an overlapping
strcpy (or multiple modifications of an object without intervening
sequence point). This code works, and valgrind doesn't complain, but
I'd
still like to know if it is guaranteed to be correct usage.

Thanks,

-David

Apr 10 '06 #1
1 1699
David Resnick wrote:
Is the assignment of a substr of the string 'a' to 'a' in the
code below OK?
Yes.
#include <string>
#include <iostream>

int main(void)
{
std::string a = "hello";
std::cout << a << std::endl;

a = a.substr(0,2);
std::cout << a << std::endl;

return 0;
}

I saw no restrictions on operator= in my STL reference (Josuttis).
But this makes me slightly nervous. I've programmed more in C
than C++, and this feels like it could be rather like an overlapping
strcpy (or multiple modifications of an object without intervening
sequence point). This code works, and valgrind doesn't complain, but
I'd
still like to know if it is guaranteed to be correct usage.


Expression 'a.substr(0,2)' creates a temporary object, which then is
assigned to 'a'. No problem whatsoever.

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: lawrence | last post by:
I've a template with some PHP code in it. I need to get the names of all the PHP commands, so I can import them and so I can make sure they are officially allowed (for security purposes, users are...
5
by: MuffinMan | last post by:
Hi, I'm trying to add up an integer with the result from substr() like $second_int = substr( .... ) + $int; I know the result from substr() is an integer and so is $int but in the end...
1
by: Jim Dawson | last post by:
I was writing a subroutine to extract fields from lines of text when I ran into an issue. I have reproduced this error on Perl 5.8 on AIX, 5.8 on Linux and 5.6 on Windows. ############### CUT...
10
by: rxl124 | last post by:
I have files that I only need one field that I need to grep it out and I am trying to assign that to another file. (It happens that one field that I am looking for has some other character that...
0
by: vanGogh | last post by:
I have generated classes based on an Xml schema file (xsd) using the XSD.exe tool. My goal is to: - write an application that can parse XML documents - fill objects (based on the generated...
2
by: lawpoop | last post by:
Hey folks - I'm working on a spellchecking routine. The pspell libraries don't handle punctuation; I have to make a script a little smarter. I'm having a problem popping English posessive...
6
by: sks | last post by:
Hi, Here is a small program that is wrtten to simply use substr. When the second parameter in substr (length of the string to be extracted) is lesser than 0, the output is the entire string (I'm...
3
by: Boltar | last post by:
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...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.