Topic: End of thread: evaluation order of arr[i]=i=j


Author: Valentin Bonnard <Bonnard.V@wanadoo.fr>
Date: 1999/10/27
Raw View
Alastair J. Houghton wrote:

> Nonesense.

Before saying that to someone like John, you should
_really_ check what you are saying.

> Both statements are well-defined;
>
>   p->next = p = 0;
>
> will cause most machines to flag an exception, because the
> evaluation order is right-to left and p has already been set
> to null.

Before posting in comp.STD.c++, you need to understand some
standards terms. In particular, undefined behaviour means
that anything can happen: you understand that the statement

  p->next = p = 0;

produces undefined behaviour 'cause a null pointer is
dereferenced. (Undefined behaviour actually comes from
the fact that p is modified and read w/o a sequence
point.)

>   a[i] = i = j;
>
> amounts to
>
>   i = j; a[j] = j;
>
> which is fine as far as I can see.

No. The behaviour is undefined in the first case. It's
will-defined in the second. You need to either:

- read the relevant parts of the standard

- read past discussions in comp.std.c++ via DejaNews (the
  problems of order of evaluation and co. have _really_
  been beaten to death)

Please don't restart the same thread for the 100th times.

> Similarly,
>
>   p = p->next = 0;
>
> is the same as
>
>   p->next = 0; p = p->next;

No

> which is also fine, as does
>
>   i = a[i] = j;
>
> which just does
>
>   a[i] = j; i = j;

No again

> Evaluation order of the "=" operator *is* defined as right-to-left.

No again

> I'm virtually certain that this is what the standard says - it's
> certainly been my understanding of the situation for several
> years.

That doesn't prove anything (well, actually it does prove that your
understanding has been completly wrong for several years).

--

Valentin Bonnard
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]