Sequence points 
Author Message
 Sequence points

I understand why c = c++; can be undefined. Modification twice within a
single sequence point, so if c is originally 9, we can do the ++c
modification before or after the = modification, and thus get results 10
or 9.

But what about c = ++c; is this also undefined?

Just guessing, I would say it depends on how we define ++c. Do we say that
++c means "first make modification to c, then evaluate c", in which case
c = ++c; where c was first 9 should always assign c the value 10.

or does ++c mean "the evaluation of the expression ++c is c+1. c still at
this point can (if it wants to) retain its value of 9. ie the
modification can take place anytime, just make sure the evaluation is the
value HAD it been modified." If so, then there would be two possibilities:

c = 9;
c = ++c;

1. first c is 9. Then an assignment of c+1 (10) is assigned to c; then ++c
is done, making c 11.
2. first c is 9. Then we evaluate ++c to be 10. Then we modify c from the
++c, so c is now 10. Then we assign 10 to c, so c is 10.

So which one is it?

Josh Waxman



Fri, 02 Jun 2000 03:00:00 GMT  
 Sequence points

Quote:

> I understand why c = c++; can be undefined. Modification twice within a
> single sequence point, so if c is originally 9, we can do the ++c
> modification before or after the = modification, and thus get results 10
> or 9.

> But what about c = ++c; is this also undefined?

> Just guessing, I would say it depends on how we define ++c. Do we say that
> ++c means "first make modification to c, then evaluate c", in which case
> c = ++c; where c was first 9 should always assign c the value 10.

There's no need to guess. The language definition makes this quite
clear. An expression that modifies a value twice without an intervening
sequence point has undefined behavior.


Fri, 02 Jun 2000 03:00:00 GMT  
 Sequence points

Quote:

> I understand why c = c++; can be undefined. Modification twice within a
> single sequence point, so if c is originally 9, we can do the ++c
> modification before or after the = modification, and thus get results 10
> or 9.

Or 42. Or 12234324. Or your hard drive can get reformated. You have
invoked undefined behavior by modifying c twice between sequence points.
Absolutely anything is OK.

Quote:
> But what about c = ++c; is this also undefined?

Yup. It doesn't matter what or how the prefix operator makes the changes.
The fact of the matter is, you have modified a variable twice between
sequence points. This is -always- undefined behavior. Always. (BTW, this
question was asked fairly recently.)

Quote:
> Just guessing, I would say it depends on how we define ++c.

Nope, it depends on how we define 'undefined behavior.'

Quote:
> Do we say that ++c means "first make modification to c, then evaluate
> c",

We say that, given 'c == x', 'c == x+1' will become true at some point in
time before the next sequence point, and that the result of the expression
is 'x+1'. c DOES NOT need to EQUAL x+1 yet; only by the next sequence
point, whenever that may be.

Quote:
> in which case c = ++c; where c was first 9 should always assign c the
> value 10.

Wrong. c can be absolutely positively anything.

Quote:
> or does ++c mean "the evaluation of the expression ++c is c+1. c still at
> this point can (if it wants to) retain its value of 9. ie the
> modification can take place anytime, just make sure the evaluation is the
> value HAD it been modified."

Right.

Quote:
> If so, then there would be two possibilities:

No, there are an infinte number of possibilites. Undefined behavior has
been invoked. Anything is valid.

[snip]

Quote:
> So which one is it?

I don't know. I vote for the hard drive reformatting case. If more
compilers did this, we might have better code being produced than we do
now.

[-                               firewind                                   -]

[-          "You're just jealous because the voices talk to -me-."          -]
[-                   Have a good day, and enjoy your C.                     -]
[-          (on a crusade of grumpiness where grumpiness is due)            -]



Fri, 02 Jun 2000 03:00:00 GMT  
 Sequence points

Quote:

> I understand why c = c++; can be undefined. Modification twice within a
> single sequence point, so if c is originally 9, we can do the ++c
> modification before or after the = modification, and thus get results 10
> or 9.
> But what about c = ++c; is this also undefined?

Consider:
c = 9;
When we do the d=++c how do we do this:

1) We can take the value of c add one and assign the result to d, now
the compiler can go back and actually increment the value of c.
2) We can increment the value of c then assign it to d.

One might assume that any compiler publisher will do the first but this
might not be the best way to do it on every single architecture.

3) We can take the value of c pass it to one processor which increments
it and assigns it to d, and pass c itself to a second processor for
incrementing its value.

The real point is that the C standard does not assume anything about the
processor.  It lets the compiler produce the optimum code for the
architecture regardless.

Besides, under any circumstances a simple

        c++;

works every time.  c gets incremented and retains the value.

Quote:
> Just guessing, I would say it depends on how we define ++c. Do we say that
> ++c means "first make modification to c, then evaluate c", in which case
> c = ++c; where c was first 9 should always assign c the value 10.
> or does ++c mean "the evaluation of the expression ++c is c+1. c still at
> this point can (if it wants to) retain its value of 9. ie the
> modification can take place anytime, just make sure the evaluation is the
> value HAD it been modified." If so, then there would be two possibilities:
> c = 9;
> c = ++c;
> 1. first c is 9. Then an assignment of c+1 (10) is assigned to c; then ++c
> is done, making c 11.
> 2. first c is 9. Then we evaluate ++c to be 10. Then we modify c from the
> ++c, so c is now 10. Then we assign 10 to c, so c is 10.
> So which one is it?

Either, or c == <enter any random value here>, or your hard disk gets
reformated, or your CD whistles Dixie.

--
****************************************************************
What is the speed of dark?
When you're sending someone Styrofoam, what do you pack it in?
Why are there Braille signs on drive-up ATM's?
How come you never hear about gruntled employees?
I have an answering machine in my car.
    It says "I'm home now.
    But leave a message and I'll call when I'm out."
=========================================

=========================================
READ THE FAQ for more information:
   C-FAQ ftp sites: ftp://ftp.eskimo.com or ftp://rtfm.mit.edu
   Hypertext C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html



Fri, 02 Jun 2000 03:00:00 GMT  
 Sequence points

On Mon, 15 Dec 1997 19:54:55 -0500, Joshua Waxman

Quote:

>1. first c is 9. Then an assignment of c+1 (10) is assigned to c; then ++c
>is done, making c 11.
>2. first c is 9. Then we evaluate ++c to be 10. Then we modify c from the
>++c, so c is now 10. Then we assign 10 to c, so c is 10.

>So which one is it?

3.

See: http://www.eskimo.com/~scs/C-faq/top.html.

==
Miguel Carrasquer Vidal                     ~ ~
Amsterdam                   _____________  ~ ~

========================== Ce .sig n'est pas une .cig



Sat, 03 Jun 2000 03:00:00 GMT  
 Sequence points



Quote:
>I understand why c = c++; can be undefined. Modification twice within a
>single sequence point, so if c is originally 9, we can do the ++c
>modification before or after the = modification, and thus get results 10
>or 9.

>But what about c = ++c; is this also undefined?

Yes, for just the same reason: you're modifying 'c' twice between
sequence points.

Here are the sequence points:
* just before calling a function, after the arguments are evaluated
* after evaluating the left-hand operand of &&, ||, ?: or ,
* at the semicolon marking the end of a statement
* at the end of an initialiser
* the end of the controlling expression of if, switch, while or do/while
* the end of each of the three expressions in a for statement
* the end of the expression in a return statement

Now, none of these is present inside the expression 'c = c++', so
the result is undefined.  'c' could come to have the value 42, or
your program could crash, or it could trash some part of your
operating system.

That's the language lawyer's answer.

Quote:
>c = 9;
>c = ++c;

>1. first c is 9. Then an assignment of c+1 (10) is assigned to c; then ++c
>is done, making c 11.
>2. first c is 9. Then we evaluate ++c to be 10. Then we modify c from the
>++c, so c is now 10. Then we assign 10 to c, so c is 10.

Either of these interpretations is reasonable.  Because the language
definition doesn't mandate either of them above the other, you can
bet your bottom dollar that somewhere, there's a compiler which
implements each.

There are probably compilers somewhere, too, which interpret this in
some weird and wonderful way you've never even dreamed of.  Because
the compiler writers just don't have to worry about what happens
if you do 'c = ++c'.

That's the pragmatist's answer.

Whichever answer you prefer, it doesn't affect the fact that 'c = ++c'
is just plain wrong.

Hope that helps.

Cheers,
Richard
--
Richard Stamp
Churchill College, Cambridge



Sat, 03 Jun 2000 03:00:00 GMT  
 Sequence points



...

Quote:
>> So which one is it?

>I don't know. I vote for the hard drive reformatting case. If more
>compilers did this, we might have better code being produced than we do
>now.

Certainly less bad code would get to market! :-)

--
-----------------------------------------


-----------------------------------------



Sat, 03 Jun 2000 03:00:00 GMT  
 Sequence points


Quote:

> I understand why c = c++; can be undefined. Modification twice within a
> single sequence point, so if c is originally 9, we can do the ++c
> modification before or after the = modification, and thus get results 10
> or 9.

> But what about c = ++c; is this also undefined?

Even the absolutely harmless looking

   c = c = 1;

is undefined. It is undefined because the C standard says so. But just in
case you think that c = ++c; should always increment c by one, an example
of a very widespread implementation where this is not the case:

Assume that the compiler knows how to produce the best possible code for x
= ++y; and that the compiler will produce the same code for c = ++c;

On the PowerPC computers, if x and y are both in registers, the best
possible code for x = ++y; is

   add   x,y,1    /* x = y + 1 */
   add   y,y,1    /* y = y + 1 */

because both adds are independent and can be done at the same time (one
cycle). The more obvious looking

   add   y,y,1    /* y = y + 1 */
   mr    x,y      /* x = y */

is slower because the second instruction has to wait until the first one
is finished (takes two cycles).

So a reasonable compiler could produce the following code for c = ++c;

   add   c,c,1    /* c = c + 1 */
   add   c,c,1    /* c = c + 1 */



Sun, 04 Jun 2000 03:00:00 GMT  
 Sequence points

Quote:

> I understand why c = c++; can be undefined. Modification twice within a
> single sequence point, so if c is originally 9, we can do the ++c
> modification before or after the = modification, and thus get results 10
> or 9.

> But what about c = ++c; is this also undefined?

I just have to ask...what led you to ask this?  A simple "c++;" will
increment your variable just fine, whats the point of attaching the
"c="???

Quote:
> Just guessing, I would say it depends on how we define ++c. Do we say that
> ++c means "first make modification to c, then evaluate c", in which case
> c = ++c; where c was first 9 should always assign c the value 10.

> or does ++c mean "the evaluation of the expression ++c is c+1. c still at
> this point can (if it wants to) retain its value of 9. ie the
> modification can take place anytime, just make sure the evaluation is the
> value HAD it been modified." If so, then there would be two possibilities:

> c = 9;
> c = ++c;

> 1. first c is 9. Then an assignment of c+1 (10) is assigned to c; then ++c
> is done, making c 11.

huh?  an assignment of c+1 (10)??? Where do you see that?

Quote:
> 2. first c is 9. Then we evaluate ++c to be 10. Then we modify c from the
> ++c, so c is now 10. Then we assign 10 to c, so c is 10.

Yes, this is what happens.  According to operator precedence what
happens is:
c=9;    No ambiguity here right?
next line - two operators and ++ has precedence, therefore next we get
++c     Increment c
  now we're left with:
c = c;

Which I'm afraid, brings me back to my first question.  What's the
point?

Phil Christensen



Thu, 15 Jun 2000 03:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Assignment and sequence points

2. Sequence Points & Side Effects

3. Sequence point question (function result semantics)

4. Sequence point or not?

5. Yet another sequence point question

6. Sequence points

7. sequence point in printf's argument list?

8. Sequence points in initializers

9. Sequence Points, Aliasing & Optimisation

10. sequence points

11. Sequence points and undefined behaviour

12. Sequence points in this code

 

 
Powered by phpBB® Forum Software