Re: Sequence point problem?

From: Jack Klein (jackklein_at_spamcop.net)
Date: 02/06/05


Date: Sun, 06 Feb 2005 03:43:52 -0600

On Sat, 5 Feb 2005 15:31:49 -0500, "Victor Bazarov"
<v.Abazarov@comAcast.net> wrote in comp.lang.c++:

> "Dave" <better_cs_now@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/~ajo/docs/FAQ-acllc.html


Relevant Pages

  • Re: Sub-sub-expression evaluation order
    ... > anything in the standard that says that it must complete (ignoring side- ... The evaluation can be implemented in any way, ... There are indeed sequence points at the beginning of each function's ... requirements of the standard and the code produces undefined behavior. ...
    (comp.lang.c)
  • Re: Sequence point problem?
    ... Jack Klein wrote: ... >>The Standard says that between sequence points (and there are none in ... > There is a sequence point in this expression, ... > condition has been evaluated but before the evaluation of the chosen ...
    (comp.lang.cpp)
  • Re: x=(x=5,11)
    ... But the standard is not ambiguous at all. ... There is a sequence point ... This was a red herring when it first entered the conversation and is ... sequence points is to insure that the evaluation is complete. ...
    (comp.lang.c)
  • Re: Defined bahavior or otherwise?
    ... >in the standard? ... Since the result cannot vary due to permissible variations in evaluation ... order between sequence points, ...
    (comp.lang.c)
  • Re: Varied results for ++ operator in VC++/Unix.
    ... > int main ... ++a" does not contain any sequence point. ... C++ standard, the behaviour is undefined, so both answers are equelly ... modified at most once by the evaluation of an expression". ...
    (microsoft.public.vc.language)