Assignment and sequence points 
Author Message
 Assignment and sequence points

Tonight I was going through the standard "i=i++ isn't doing what I
expect" routine with a friend of mine.  She accepted that assignment
isn't a sequence point readily enough, but I couldn't give her an
answer when she asked *why* it isn't.  It had simply never occurred to
me to wonder about that before, but now I am.  Does anyone know why
`=' isn't a sequence point, and if there's any code (with defined
behavior) that would change behavior if it were?

For what it's worth, my best guess is "so that compilers can optimize
better."
--
Robert Macomber

--



Sun, 28 Dec 2003 23:25:28 GMT  
 Assignment and sequence points

Quote:
> For what it's worth, my best guess is "so that compilers can optimize
> better."

Sorry to follow up on myself, but it strikes me that a better answer
is "because assignment is just another expression operator in C, not a
special sort of statement"; even if you make `=' a sequence point,
something like "i + i++" would still have undefined behavior (though
that one doesn't seem, IME, to trip up C newbies as much as the
assignment thing - possibly because C newbies [or, most of the time,
this C not-so-newbie] don't think of "a=b" as an expression in its own
right).
--
Robert Macomber

--



Mon, 29 Dec 2003 23:52:08 GMT  
 Assignment and sequence points


Quote:
>Tonight I was going through the standard "i=i++ isn't doing what I
>expect" routine with a friend of mine.  She accepted that assignment
>isn't a sequence point readily enough, but I couldn't give her an
>answer when she asked *why* it isn't.  It had simply never occurred to
>me to wonder about that before, but now I am.  Does anyone know why
>`=' isn't a sequence point, and if there's any code (with defined
>behavior) that would change behavior if it were?

Providing examples that will convince those that have never dabbled at
hardware level is not that easy. However here is a scenario: Certain old
CPU's needed to put a wait state in whenever they wrote to memory before
writing again to that same location (it allowed the memory to
stabilise). The rule used by C means that such wait states need only be
placed at sequence points. Without the guarantee given by C a wait state
would have to be added at every assignment.

I suspect, but do not know, that similar problems could be generated in
modern highly pipelined code.

Francis Glassborow      ACCU
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
--



Mon, 29 Dec 2003 23:57:52 GMT  
 Assignment and sequence points

Quote:

> Tonight I was going through the standard "i=i++ isn't doing what I
> expect" routine with a friend of mine.  She accepted that assignment
> isn't a sequence point readily enough, but I couldn't give her an
> answer when she asked *why* it isn't.  It had simply never occurred to
> me to wonder about that before, but now I am.  Does anyone know why
> `=' isn't a sequence point, and if there's any code (with defined
> behavior) that would change behavior if it were?

> For what it's worth, my best guess is "so that compilers can optimize
> better."

Step back and think about it...  The assignment, by its very nature, cannot
be a sequence point, as you need to use the left- and right-hand sides at
the same time.  You cannot be finished using the lvalue on the left before
you have done the assignment, which cannot be done until the rvalue has
been calculated.  (Unless you are going to make a special case for the "="
operator, making it a sequence point whose left-hand side is not evaluated
until everything to its right [but before the next sequence point] has been
evaluated.)

--

+---------+----------------------------------+-----------------------------+

|    J.   |                                  |  herein are not necessarily |
|  Brody  | http://www.bestweb.net/~kenbrody |  those of fP Technologies." |
+---------+----------------------------------+-----------------------------+
GCS (ver 3.12) d- s+++: a C++$(+++) ULAVHSC^++++$ P+>+++ L+(++) E-(---)

    DI+(++++) D---() G e* h---- r+++ y?
--



Mon, 29 Dec 2003 23:58:07 GMT  
 Assignment and sequence points
On 11 Jul 2001 15:25:28 GMT, "Robert J. Macomber"

Quote:
> Tonight I was going through the standard "i=i++ isn't doing what I
> expect" routine with a friend of mine.  She accepted that assignment
> isn't a sequence point readily enough, but I couldn't give her an
> answer when she asked *why* it isn't.  It had simply never occurred to
> me to wonder about that before, but now I am.  Does anyone know why
> `=' isn't a sequence point, and if there's any code (with defined
> behavior) that would change behavior if it were?

> For what it's worth, my best guess is "so that compilers can optimize
> better."

How about this one:

If an assignment operator were a sequence point the code still
wouldn't work the way you probably wanted it to.

To illustrate, let's use a slightly different example:

int ar[10];
int i;

for (i = 0; i < 10; ++i)
{
   ar[i] = ++i;

Quote:
}

Sequence points work literally left to right in an example like this.
If the "=" sign were a sequence point the side effect of assigning to
ar[i] (that is, the actual write to memory) would need to be performed
_before_ the expression ++i was evaluated at all, because its side
effect (modifying the value of i) cannot take place until _after_ the
sequence point.

So what would making the unary and binary assignment operators
sequence points actually accomplish, except making them useless if the
computation of the rvalue on the right hand side has side effects?

--
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++ ftp://snurse-l.org/pub/acllc-c++/faq
--



Mon, 29 Dec 2003 23:58:17 GMT  
 Assignment and sequence points

Quote:

> Does anyone know why `=' isn't a sequence point, ...

Yeah -- it's so compilers can generate more efficient code
(think: cache in register).
--



Mon, 29 Dec 2003 23:58:40 GMT  
 Assignment and sequence points

Quote:

> ...  The assignment, by its very nature, cannot be a sequence point,

We could certainly have required that there be a sequence point
after every assignment.  "But that would be wrong."
--



Wed, 31 Dec 2003 04:19:03 GMT  
 Assignment and sequence points

A quite lovely discussion of sequence points is at:

http://www.purdue.edu/PUCC/Short-Courses/c.files/p_00267.html

Moderator: Please forgive my long signature.

--
I have watched kids testifying before Congress. It is clear that they
are completely unaware of the seriousness of their acts. There is
obviously a cultural gap. The act of breaking into a computer system
has to have the same social stigma as breaking into a neighbor's house.
It should not matter that the neighbor's door is unlocked. The press
must learn that misguided use of a computer is no more amazing than
drunk driving of an automobile.

Ken Thompson Sept. 1995 ACM



--



Sat, 03 Jan 2004 09:32:44 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Sequence Points & Side Effects

2. Sequence point question (function result semantics)

3. Sequence point or not?

4. Yet another sequence point question

5. Sequence points

6. sequence point in printf's argument list?

7. Sequence points in initializers

8. Sequence Points, Aliasing & Optimisation

9. sequence points

10. Sequence points and undefined behaviour

11. Sequence points in this code

12. Sequence points

 

 
Powered by phpBB® Forum Software