Topic: value of x=x-- ?


Author: dietmar_kuehl@yahoo.com (Dietmar Kuehl)
Date: Wed, 7 Nov 2001 18:26:05 GMT
Raw View
Hi,
wunderschnott@yahoo.com (Wunder Schnott) wrote:
> Stroustrup's "The C++ Programming Language", 3rd edition, 3rd
> printing, Sept 1997, says on page 125
> "... y=x++ is equivalent to y=(t=x,x+=1,t) ...".

This is not correct: These expressions are not at all equivalent! The latter
(sub-) expression introduces two sequence points while the former has no
sequence point at all (assuming that 'x', 'y', and 't' are built-in types).
A better match is something like

  x = (y = x) + 1

which is semantically equivalent (ie. this also results in undefined behavior
if 'x' and 'y' are actually the same object). However, the actual behavior is
probably different anyway: often processors have special instructions to
increment eg. an address and dereference the original pointer.

For non-built-in types Stroustrups approach is what is often done, ie:

  T T::operator++ (int) {
    T rc(*this);
    operator++();
    return rc;
  }

> My question: is x=x++ equivalent to x=(t=x,x+=1,t)?

If 'x' is an object of built-in type, it is not equivalent: The former
expression modifies 'x' twice without intervening sequence points, thus
resulting in undefined behavior. The latter expression avoids this problem
because the comma operator introduces a sequence point. If 'x' is not of a
built-in type, the equivalence depends on the how the operators are
overloaded...

> When I compile the code
> int x=0; x=x--; cout<<x;
> using gcc 2.96-98 (RedHat's gcc) I get -1.  If x=x-- is equivalent to
> x=(t=x,x-=1,t) then the output should be zero.  Which one should it
> be?

Is may also be 4711, crash your computer, cause daemons to fly out of your
nouse (although this effect is pretty unlikely...), etc. It is just undefined
behavior if you change an object twice without intervening sequence point.
--
<mailto:dietmar_kuehl@yahoo.com> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>

---
[ 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://www.research.att.com/~austern/csc/faq.html                ]





Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Wed, 7 Nov 2001 18:28:39 GMT
Raw View
In article <9s9uq3$gjh$1@spacebar.ucc.usyd.edu.au>, Andrew Maclean
<a.maclean_remove_this_@acfr.usyd.edu.au> writes
>The compiler is correct and so is Stroustrup.
>x = 0;
>x--; // Implies x == -1.
>t=x;
>x = t;
>(...,...,...) is executed right to left.
>I think you are executing it left to right in your mind.

Not in any book I have ever read. Indeed the comma operator has an even
stronger guarantee in the it requires that the left operand be fully
evaluated (including side effects) before the right operand is touched.

Francis Glassborow
I offer my sympathy and prayers to all those who are suffering
as a result of the events of September 11 2001.

---
[ 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://www.research.att.com/~austern/csc/faq.html                ]





Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: Wed, 7 Nov 2001 18:28:27 GMT
Raw View
Andrew Maclean wrote:
>
> The compiler is correct and so is Stroustrup.

Well, the compiler couldn't exactly get it wrong. After all, it's
undefined behavior.

---
[ 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://www.research.att.com/~austern/csc/faq.html                ]





Author: Jack Klein <jackklein@spamcop.net>
Date: Fri, 9 Nov 2001 08:18:24 GMT
Raw View
On Wed,  7 Nov 2001 16:08:45 GMT, Yamamoto Hirotaka
<ymmt@yc5.so-net.ne.jp> wrote in comp.std.c++:

> wunderschnott@yahoo.com (Wunder Schnott) writes:
>
> > My question: is x=x++ equivalent to x=(t=x,x+=1,t)?
>
> no.  it is equivalent to:  (x = x, x++)
> likewise, x=++x equals to: (++x, x = x)
>
> in short, all of them are actually equivalent.

Assuming x is a scalar type...

No, the behavior of x=x++ is undefined.

The behavior of x=++x is another story.  There is some badly confused
wording in the ISO C standard that could be interpreted to make it
well defined.

++x is required to return the lvalue of the result of incrementing x.
It can hardly do this without storing the new incremented value first.
The assignment to the left hand side obviously can't take place until
the lvalue of the right hand side is available, the prefix increment
must be completed before the assignment is made.

At least until that mess get fixed, one can make a case that indeed
the expression:

   x=++x;

....is the equivalent of ++x, x = x.

That's the problem with certain operators imposing sequences of
writing values to memory despite not being defined as sequence points.

--
Jack Klein
Home: http://JK-Technology.Com

---
[ 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://www.research.att.com/~austern/csc/faq.html                ]





Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Fri, 9 Nov 2001 23:38:45 GMT
Raw View
In article <v10nutcq8tdduq44q2f18tucm9jlkt2h9t@4ax.com>, Jack Klein
<jackklein@spamcop.net> writes
>++x is required to return the lvalue of the result of incrementing x.
>It can hardly do this without storing the new incremented value first.
>The assignment to the left hand side obviously can't take place until
>the lvalue of the right hand side is available, the prefix increment
>must be completed before the assignment is made.
>
>At least until that mess get fixed, one can make a case that indeed
>the expression:
>
>   x=++x;
>
>....is the equivalent of ++x, x = x.
>
>That's the problem with certain operators imposing sequences of
>writing values to memory despite not being defined as sequence points.

But I am not convinced that that is the case. All that is required is
that the compiler can determine the rvalue that would result from an
lvalue to rvalue conversion. It does not have to complete the write in
order to do that, and requiring it to do so could require
inefficiencies. When written as blatantly as:

x=++x;
It is hard to see any compiler getting it wrong but what about:

void foo(int& a, int& b){
  a=++b;

//Must the compiler assume aliasing?
}

int main(){
  int i=0;
  foo(i, i);
}



Francis Glassborow
I offer my sympathy and prayers to all those who are suffering
as a result of the events of September 11 2001.

---
[ 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://www.research.att.com/~austern/csc/faq.html                ]





Author: wunderschnott@yahoo.com (Wunder Schnott)
Date: Tue, 6 Nov 2001 23:43:02 GMT
Raw View
Stroustrup's "The C++ Programming Language", 3rd edition, 3rd
printing, Sept 1997, says on page 125
"... y=x++ is equivalent to y=(t=x,x+=1,t) ...".
My question: is x=x++ equivalent to x=(t=x,x+=1,t)?
When I compile the code
int x=0; x=x--; cout<<x;
using gcc 2.96-98 (RedHat's gcc) I get -1.  If x=x-- is equivalent to
x=(t=x,x-=1,t) then the output should be zero.  Which one should it
be?

Thanks,

Schnott

---
[ 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://www.research.att.com/~austern/csc/faq.html                ]





Author: "Anthony Williams" <anthwil@nortelnetworks.com>
Date: Wed, 7 Nov 2001 10:53:55 GMT
Raw View
"Wunder Schnott" <wunderschnott@yahoo.com> wrote in message
news:b38f74cb.0111061247.8313c6b@posting.google.com...
> Stroustrup's "The C++ Programming Language", 3rd edition, 3rd
> printing, Sept 1997, says on page 125
> "... y=x++ is equivalent to y=(t=x,x+=1,t) ...".
> My question: is x=x++ equivalent to x=(t=x,x+=1,t)?

No.

> When I compile the code
> int x=0; x=x--; cout<<x;
> using gcc 2.96-98 (RedHat's gcc) I get -1.  If x=x-- is equivalent to
> x=(t=x,x-=1,t) then the output should be zero.  Which one should it
> be?

5p4:
"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."

There are no sequence points in "x=x--", and an attempt is made to modify x
twice --- once by the post-decrement, once by the assignment. This is
therefore undefined behaviour, and anything goes.

Anthony
--
Anthony Williams
Software Engineer, Nortel Networks Optical Components Ltd
The opinions expressed in this message are not necessarily those of my
employer



---
[ 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://www.research.att.com/~austern/csc/faq.html                ]





Author: "Andrew Maclean" <a.maclean_remove_this_@acfr.usyd.edu.au>
Date: Wed, 7 Nov 2001 16:08:40 GMT
Raw View
The compiler is correct and so is Stroustrup.
x = 0;
x--; // Implies x == -1.
t=x;
x = t;
(...,...,...) is executed right to left.
I think you are executing it left to right in your mind.

Andrew


"Wunder Schnott" <wunderschnott@yahoo.com> wrote in message
news:b38f74cb.0111061247.8313c6b@posting.google.com...
> Stroustrup's "The C++ Programming Language", 3rd edition, 3rd
> printing, Sept 1997, says on page 125
> "... y=x++ is equivalent to y=(t=x,x+=1,t) ...".
> My question: is x=x++ equivalent to x=(t=x,x+=1,t)?
> When I compile the code
> int x=0; x=x--; cout<<x;
> using gcc 2.96-98 (RedHat's gcc) I get -1.  If x=x-- is equivalent to
> x=(t=x,x-=1,t) then the output should be zero.  Which one should it
> be?
>
> Thanks,
>
> Schnott
>
> ---
> [ 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://www.research.att.com/~austern/csc/faq.html                ]
>


---
[ 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://www.research.att.com/~austern/csc/faq.html                ]





Author: Yamamoto Hirotaka <ymmt@yc5.so-net.ne.jp>
Date: Wed, 7 Nov 2001 16:08:45 GMT
Raw View
wunderschnott@yahoo.com (Wunder Schnott) writes:

> My question: is x=x++ equivalent to x=(t=x,x+=1,t)?

no.  it is equivalent to:  (x = x, x++)
likewise, x=++x equals to: (++x, x = x)

in short, all of them are actually equivalent.

---
[ 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://www.research.att.com/~austern/csc/faq.html                ]





Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Wed, 7 Nov 2001 16:08:51 GMT
Raw View
In article <b38f74cb.0111061247.8313c6b@posting.google.com>, Wunder
Schnott <wunderschnott@yahoo.com> writes
>Stroustrup's "The C++ Programming Language", 3rd edition, 3rd
>printing, Sept 1997, says on page 125
>"... y=x++ is equivalent to y=(t=x,x+=1,t) ...".
>My question: is x=x++ equivalent to x=(t=x,x+=1,t)?
>When I compile the code
>int x=0; x=x--; cout<<x;
>using gcc 2.96-98 (RedHat's gcc) I get -1.  If x=x-- is equivalent to
>x=(t=x,x-=1,t) then the output should be zero.  Which one should it
>be?

Either, or anything else including set fire to your monitor. In
evaluating x=x--; the storage for x is twice written to without an
intervening sequence point. That is why Bjarne was stretching a point
with his explanation because the comma operator introduces sequence
points that do not exist in the original statement.


Francis Glassborow
I offer my sympathy and prayers to all those who are suffering
as a result of the events of September 11 2001.

---
[ 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://www.research.att.com/~austern/csc/faq.html                ]





Author: Michiel Salters<Michiel.Salters@cmg.nl>
Date: Wed, 7 Nov 2001 16:09:51 GMT
Raw View
In article <b38f74cb.0111061247.8313c6b@posting.google.com>, Wunder Schnott
says...
>
>Stroustrup's "The C++ Programming Language", 3rd edition, 3rd
>printing, Sept 1997, says on page 125
>"... y=x++ is equivalent to y=(t=x,x+=1,t) ...".
>My question: is x=x++ equivalent to x=(t=x,x+=1,t)?

No. It can be equal to x=42, or x=FormatHardDisk();
although the latter is somewhat less likely. The reason is that
x=x++; attempts to modify x twice between sequence points [1].
There first sequence point is before the statement, the second
at the ';', and there is both an increment and an assignment to
x in between. In C++, that means they would happen at the same
time. That is of course not possible, so it is left entirely
open what happens. Each compiler may decide.

If this seems silly, consider
void f( int* p, int *q ) {
*p = *q++;
}

The logical code generated for this causes problems if p==q,
but if the compiler had to insert a test to check if p==q this
code would be slower. So, in the spirit of C++, this check is
left to the programmer.

>When I compile the code
>int x=0; x=x--; cout<<x;
>using gcc 2.96-98 (RedHat's gcc) I get -1.  If x=x-- is equivalent to
>x=(t=x,x-=1,t) then the output should be zero.  Which one should it
>be?

There is no official correct answer, so -1 is Ok. Apparently g++ used something
like x=x; x-=1; which is a legal implementation of x=x--;

HTH,
Michiel Salters
-----
[1] if x is int. If x isn't a builtin type but (say) mynum::BigInt, you're
right. User-defined types have a sequence point before assignment happens.

--
Michiel Salters
Consultant Technical Software Engineering
CMG Trade, Transport & Industry
Michiel.Salters@cmg.nl

---
[ 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://www.research.att.com/~austern/csc/faq.html                ]