taras.diakiw
12/8/2008 3:53:00 PM
Hi everyone,
First off, thanks for all of the detailed replies. It's taken me a
while to digest the information, and I've decided to write a few
separate responses in an effort to partition the discussion into
separate ideas.
First off, the mention of sequence points. I understanding the postfix
increment operator (and equivalenty the prefix increment operator)
very roughly - "'use' first, increment later" (I'm aware that this is
over-simplified, and thus is most probably not entirely correct, and
the term 'use' is not precisely defined). If we think of a postfix
increment operator being implemented as a function, then the pseudo-
code would look something like:
* store copy of argument
* increment argument
* return copy of argument
In this case, a statement like:
i = i++;
would be well defined, as regardless of whether the LHS or the RHS is
evaluated first, at the end of the statement the LHS will 'receive'
the old value of i (the LHS evaluates to a l-value, the RHS evaluates
to the old value of i).
However, obviously this is incorrect, which implies that my
understanding of the postfix increment operator is not complete, and
my pseudo-code is not precisely what happens in reality. I believe
that this concept of 'sequence points' has something to do with it :).
What are these sequence points, and how does their use/implementation
differ to my 'pseudo-code' above? Also, would you have the same
problems with UDTs (as the operators are actually functions, so my
psuedo-code would be closer to what is actually happening)?
I have included the previous snippets relating to 'sequence points'
below.
Cheers
Taras
On Nov 23, 9:36 pm, Andrey Tarasevich <andreytarasev...@hotmail.com>
wrote:
> Taras_96 wrote:
> > ...
> ...
> > v[i] = v[i++];
>
> > is undefined: "may be evaluated as either v [1 ]=1 or v [2 ]=1 or may
> > cause some even stranger behavior."
>
> > This comes as a surprise, as I would have thought that the RHS of an
> > assignment operator would be evaluated before the LHS!
>
> Why? Firstly, there's absolutely no reason to evaluate RHS before LHS.
> Secondly, the big problem here is the side effect of 'i++'. Side effects
> can take place at any moment before the next sequence point, which
> ruins the above regardless of the order of evaluation.
>
On Nov 24, 10:43 am, James Kanze <james.ka...@gmail.com> wrote:
> On Nov 23, 8:39 pm, Taras_96 <taras...@gmail.com> wrote:
> ...
>
> No. A compiler is allowed to evaluate all of the functions
> first, then do the two additions. The only things constraining
> ordering are direct dependencies (f() and g() must both be
> evaluated before their results are added) and something called
> sequence points. Sequence points only introduce a partial
> ordering, however, and very few operators induce a sequence
> point.
>
> ....
> The second is that the language imposes some constraints
> regarding what a legal program can do in an expression. In
> particular "between the previous and next sequence point a
> scalar object shall have its stored value modified at most once
> by the evaluation of an expression. Furthermore, the prior
> value shall be accessed only to determine the value to be
> stored." Otherwise, you get undefined behavior. Since your
> expression contains no sequence points, it's undefined behavior.
> (I've probably forgotten some exotic cases, but the end of the
> full expression, the &&, || ?: and comma operators, and a
> function call or a return are sequence points. Note that they
> don't necessarily impose a full ordering; a compiler can still
> intermingle the evaluation of a functions arguments with other
> parts of the expression; all of the side effects of evaluating
> the function's arguments must occur before the function is
> called, however.)
>
....
> Everything concerning ordering constraints, etc., has recently
> been rewritten to take multithreaded environments into
> consideration; the next version of the standard uses the concept
> of sequencing (an operation is sequenced before, unsequenced or
> indeterminately sequenced), rather than sequence points. I've
> not read it in enough detail to be sure, but I think it will
> result in the above being defined.