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

Sequence point problem?

Hello all,

Can anybody identify any sequence point problems in the expression below?

i = (i == string::npos) ? 0 : ++i;

Thanks,
Dave
Jul 23 '05 #1
3 1176
"Dave" <be***********@yahoo.com> wrote...
Can anybody identify any sequence point problems in the expression below?

i = (i == string::npos) ? 0 : ++i;


The Standard says that between sequence points (and there are none in
this expression) a stored value of any object shall be modified at most
once. If the expression in the parentheses yields false, 'i' will be
modified twice, once by ++ and the other time with the assignment, which
causes undefined behaviour. If you rewrite this as

i = (i == string::npos) ? 0 : i + 1;

there would be no problem.

V
Jul 23 '05 #2
On Sat, 5 Feb 2005 15:31:49 -0500, "Victor Bazarov"
<v.********@comAcast.net> wrote in comp.lang.c++:
"Dave" <be***********@yahoo.com> wrote...
Can anybody identify any sequence point problems in the expression below?

i = (i == string::npos) ? 0 : ++i;


The Standard says that between sequence points (and there are none in
this expression) a stored value of any object shall be modified at most
once. If the expression in the parentheses yields false, 'i' will be
modified twice, once by ++ and the other time with the assignment, which
causes undefined behaviour. If you rewrite this as

i = (i == string::npos) ? 0 : i + 1;

there would be no problem.


There is a sequence point in this expression, at the '?' after the
condition has been evaluated but before the evaluation of the chosen
predicate expression.

But your reasoning and correction are right.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 23 '05 #3
Jack Klein wrote:
On Sat, 5 Feb 2005 15:31:49 -0500, "Victor Bazarov"
<v.********@comAcast.net> wrote in comp.lang.c++:

"Dave" <be***********@yahoo.com> wrote...
Can anybody identify any sequence point problems in the expression below?

i = (i == string::npos) ? 0 : ++i;


The Standard says that between sequence points (and there are none in
this expression) a stored value of any object shall be modified at most
once. If the expression in the parentheses yields false, 'i' will be
modified twice, once by ++ and the other time with the assignment, which
causes undefined behaviour. If you rewrite this as

i = (i == string::npos) ? 0 : i + 1;

there would be no problem.

There is a sequence point in this expression, at the '?' after the
condition has been evaluated but before the evaluation of the chosen
predicate expression.


Thank you for the correction, Jack. So, even though there is a SeqP in
that expression, ++i would be evaluated *after* it, right? Which still
makes this essentially the same as

if (i == string::npos)
i = 0;
else
i = ++i; // undefined behaviour

V
Jul 23 '05 #4

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

Similar topics

3
by: Sensorflo | last post by:
After browsing though many newsgroups articels I'm still not shure how operator precedence, operator associativity, sequence points, side effects go together. Currently I have the following view: ...
3
by: sugaray | last post by:
Can somebody explain to me what is sequence point ? With few examples would be even better. Thanx for your help.
53
by: Deniz Bahar | last post by:
I know the basic definition of a sequence point (point where all side effects guaranteed to be finished), but I am confused about this statement: "Between the previous and next sequence point an...
4
by: Eric E | last post by:
Hi, I have a question about sequences. I need a field to have values with no holes in the sequence. However, the values do not need to be in order. My users will draw a number or numbers from...
3
by: kevin | last post by:
Is that even possible? I am creating a web service in .NET to expose some already created .NET programs to other groups. One group is writing the client in PERL, and thus wishes the wsdl schema...
8
by: regis | last post by:
Greetings, about scanf matching nonempty sequences using the "%" matches a nonempty sequence of anything except '-' "%" matches a nonempty sequence of anything except ']" matches a nonempty...
9
by: John Smith | last post by:
I've been playing with splint, which returns the following warning for the code below: statlib.c: (in function log_norm_pdf) statlib.c(1054,31): Expression has undefined behavior (left operand...
4
by: Tomás Ó hÉilidhe | last post by:
I'm writing a program currently that was working perfectly until I decided to compile it with "-O3" in gcc (-O3 specifies optimisation of the third level). Anyway, I found the problem. I had the...
21
by: bilgekhan | last post by:
After doing a succcessful insert() or find() on a set<Tcontainer is it possible to get the item number of this item in the set? (ie. its zero-based sequence number (position/location/rank/index)...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.