Topic: Postfix ++ operator


Author: allan_w@my-dejanews.com (Allan W)
Date: Tue, 25 Feb 2003 16:24:41 CST
Raw View
thomas@iai.fzk.de ("Thomas Beckmann") wrote
> Hi,
>
> > MYDATA x=0;
> > x=x++;
> >
> > as long as MYDATA is a builtin type, x = 1
> > but according to Stroustrup, operator++ is equivalent to
> > y = src behaves as (tmp=src, src+=1, tmp)
> > so
> > x=x++ should leave x as 0
> >
> > So this creates an inconsistency between builtin types and formal
> > definition.
> >
> > Suggestions ?
> >
> > Best regards,
>
> ha, I know this one:
> you are changing the same variable twice in one statement. The results of
> that are undefined! However, I don't know the standard's paragraph anymore.
> Was in the newsgroup a year ago.

It is still in the FAQ for comp.lang.c at
http://www.eskimo.com/~scs/C-faq/top.html .
Most of what's in that FAQ applies equally well to C++.
Alvaro, please read section 3 at once!

---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Michiel.Salters@cmg.nl (Michiel Salters)
Date: Mon, 17 Feb 2003 17:11:01 +0000 (UTC)
Raw View
ALVAROSAUCA@t-online.de ("Alvaro Sauca") wrote in message news:<b2bg7e$9il$01$1@news.t-online.com>...

[undefined behavior for x=x++]

>     I know this is a bad practice, but this issue arose by chance using
> SystemC (a C++ "class" for circuit design (www.systemc.org) ) , where a
> SystemC standard integer type behaved differently from standard builtin
> integer, and the doubt on how to properly implement this operator inside
> a class appeared.

Well, the standard says that any behavior is OK. So if your initial
implementation happens to crash, it's technically OK. The reason the
standard says this is exactly to let implementors know they don't have
to worry. And if you want to mimick such an integer, don't worry either.

HTH,
--
Michiel Salters

---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]





Author: francis.glassborow@ntlworld.com (Francis Glassborow)
Date: Wed, 12 Feb 2003 20:07:55 +0000 (UTC)
Raw View
In article <b2bg7e$9il$01$1@news.t-online.com>, Alvaro Sauca
<ALVAROSAUCA@t-online.de> writes
>    Great! ; ) So this is what I wanted to know .. so is it stated clearly
>in the standard that this will have undefined behaviour ?

Clause 5, para 4. The subsequent example (which is non-normative), is
wrong because it uses the term 'unspecified' when it should use
'undefined'. Side effects between sequence points can interleave which
is why writing twice to the same variable is undefined rather than
unspecified behaviour.




--
ACCU Spring Conference 2003 April 2-5
The Conference you cannot afford to miss
Check the details: http://www.accuconference.co.uk/
Francis Glassborow      ACCU

---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]





Author: ALVAROSAUCA@t-online.de ("Alvaro Sauca")
Date: Wed, 12 Feb 2003 21:58:57 +0000 (UTC)
Raw View
Thanks a lot. : )

"Francis Glassborow" <francis.glassborow@ntlworld.com> escribi=F3 en el
mensaje news:8v2OtTE3giS+Ew0i@robinton.demon.co.uk...
> In article <b2bg7e$9il$01$1@news.t-online.com>, Alvaro Sauca
> <ALVAROSAUCA@t-online.de> writes
> >    Great! ; ) So this is what I wanted to know .. so is it stated
clearly
> >in the standard that this will have undefined behaviour ?
>
> Clause 5, para 4. The subsequent example (which is non-normative), is
> wrong because it uses the term 'unspecified' when it should use
> 'undefined'. Side effects between sequence points can interleave which
> is why writing twice to the same variable is undefined rather than
> unspecified behaviour.
>
>
>
>
> --
> ACCU Spring Conference 2003 April 2-5
> The Conference you cannot afford to miss
> Check the details: http://www.accuconference.co.uk/
> Francis Glassborow      ACCU
>
> ---
> [ 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.jamesd.demon.co.uk/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.jamesd.demon.co.uk/csc/faq.html                       ]





Author: ALVAROSAUCA@t-online.de ("Alvaro Sauca")
Date: Tue, 11 Feb 2003 16:14:25 +0000 (UTC)
Raw View
MYDATA x=0;
x=x++;

as long as MYDATA is a builtin type, x = 1
but according to Stroustrup, operator++ is equivalent to
y = src behaves as (tmp=src, src+=1, tmp)
so
x=x++ should leave x as 0

So this creates an inconsistency between builtin types and formal
definition.

Suggestions ?

Best regards,





---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]





Author: francis.glassborow@ntlworld.com (Francis Glassborow)
Date: Tue, 11 Feb 2003 18:33:06 +0000 (UTC)
Raw View
In article <b2aag6$8nv$05$1@news.t-online.com>, Alvaro Sauca
<ALVAROSAUCA@t-online.de> writes
>MYDATA x=0;
>x=x++;
>
>as long as MYDATA is a builtin type, x = 1
>but according to Stroustrup, operator++ is equivalent to
>y = src behaves as (tmp=src, src+=1, tmp)
>so
>x=x++ should leave x as 0
>
>So this creates an inconsistency between builtin types and formal
>definition.

No it does not. You have undefined behaviour because you write to x
twice between sequence points. Not only does it not have to be 0 it can
set your monitor on fire or anything else. Of course usually you get
reasonable behaviour but there is no requirement from the Standard which
simply says 'DO NOT DO THAT.'

--
ACCU Spring Conference 2003 April 2-5
The Conference you cannot afford to miss
Check the details: http://www.accuconference.co.uk/
Francis Glassborow      ACCU

---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]





Author: ALVAROSAUCA@t-online.de ("Alvaro Sauca")
Date: Wed, 12 Feb 2003 04:04:53 +0000 (UTC)
Raw View
    Great! ; ) So this is what I wanted to know .. so is it stated clearl=
y
in the standard that this will have undefined behaviour ?

    I know this is a bad practice, but this issue arose by chance using
SystemC (a C++ "class" for circuit design (www.systemc.org) ) , where a
SystemC standard integer type behaved differently from standard builtin
integer, and the doubt on how to properly implement this operator inside =
a
class appeared.

    Best regards,

Alvaro Sauca


"Francis Glassborow" <francis.glassborow@ntlworld.com> escribi=F3 en el
mensaje news:m7QII6UtwSS+Ew3w@robinton.demon.co.uk...
> In article <b2aag6$8nv$05$1@news.t-online.com>, Alvaro Sauca
> <ALVAROSAUCA@t-online.de> writes
> >MYDATA x=3D0;
> >x=3Dx++;
> >
> >as long as MYDATA is a builtin type, x =3D 1
> >but according to Stroustrup, operator++ is equivalent to
> >y =3D src behaves as (tmp=3Dsrc, src+=3D1, tmp)
> >so
> >x=3Dx++ should leave x as 0
> >
> >So this creates an inconsistency between builtin types and formal
> >definition.
>
> No it does not. You have undefined behaviour because you write to x
> twice between sequence points. Not only does it not have to be 0 it can
> set your monitor on fire or anything else. Of course usually you get
> reasonable behaviour but there is no requirement from the Standard whic=
h
> simply says 'DO NOT DO THAT.'
>
> --
> ACCU Spring Conference 2003 April 2-5
> The Conference you cannot afford to miss
> Check the details: http://www.accuconference.co.uk/
> Francis Glassborow      ACCU
>
> ---
> [ 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.jamesd.demon.co.uk/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.jamesd.demon.co.uk/csc/faq.html                       ]





Author: thomas@iai.fzk.de ("Thomas Beckmann")
Date: Wed, 12 Feb 2003 04:05:34 +0000 (UTC)
Raw View
Hi,


> MYDATA x=0;
> x=x++;
>
> as long as MYDATA is a builtin type, x = 1
> but according to Stroustrup, operator++ is equivalent to
> y = src behaves as (tmp=src, src+=1, tmp)
> so
> x=x++ should leave x as 0
>
> So this creates an inconsistency between builtin types and formal
> definition.
>
> Suggestions ?
>
> Best regards,


ha, I know this one:
you are changing the same variable twice in one statement. The results of
that are undefined! However, I don't know the standard's paragraph anymore.
Was in the newsgroup a year ago.


Regards,
        Thomas.


---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]





Author: v.Abazarov@attAbi.com ("Victor Bazarov")
Date: Wed, 12 Feb 2003 04:22:53 +0000 (UTC)
Raw View
"Alvaro Sauca" <ALVAROSAUCA@t-online.de> wrote...
>
> MYDATA x=0;
> x=x++;
>
> as long as MYDATA is a builtin type, x = 1


No.  For x=x++ causes undefined behaviour.  See 5/4.  You attempt
to modify the stored value more than once between sequence points.

Victor
--
Please remove capital A's from my address when replying by mail


---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]





Author: hyrosen@mail.com (Hyman Rosen)
Date: Wed, 12 Feb 2003 04:22:59 +0000 (UTC)
Raw View
Alvaro Sauca wrote:
> MYDATA x=0;
> x=x++;
> as long as MYDATA is a builtin type, x = 1

No. This code has undefined behavior, because it contains two
modifications of x without an intervening sequence point.

---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]