This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
RE: warning: operation on 'zero' may be undefined
- From: "Dave Korn" <dk at artimi dot com>
- To: "'Paul Brook'" <paul at codesourcery dot com>,<gcc at gcc dot gnu dot org>
- Cc: "'Mathieu Malaterre'" <mmalater at nycap dot rr dot com>
- Date: Fri, 1 Oct 2004 09:59:54 +0100
- Subject: RE: warning: operation on 'zero' may be undefined
> -----Original Message-----
> From: gcc-owner On Behalf Of Paul Brook
> Sent: 01 October 2004 04:12
> On Friday 01 October 2004 03:57, Mathieu Malaterre wrote:
> > Hello,
> >
> > I am trying gcc4, and turning -Wall reveal a strange warning:
> >
> > int main()
> > {
> > int zero = 125;
> > zero = (++zero)%3;
> > }
> >
> > g++ -Wall foo.cxx
> >
> > foo.cxx: In function `int main()':
> > foo.cxx:4: warning: operation on 'zero' may be undefined
> >
> > What does this mean ?
>
> You incrementing and assigning to the same variable in one
> statement, which
> invokes undefined behaviour.
>
> The code you wrote can be interpreted as
> zero = zero%3;
> or
> zero = (zero % 3) + 1
> or possibly even something entirely different.
I'm nitpicking, but considering it's a pre-increment, I'd have said
zero = (zero + 1) % 3;
or
zero = zero + 1;
were the interpretations. And the problem is the lack of a sequence point between
them.
cheers,
DaveK
--
Can't think of a witty .sigline today....