public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: David Korn <dkorn@pixelpower.com>
To: 'Uros Bizjak' <uros@kss-loka.si>, gcc@gcc.gnu.org
Subject: RE: Is this a gcc bug?
Date: Thu, 11 Jan 2001 03:04:00 -0000	[thread overview]
Message-ID: <718D38CAB6E0D011B2C90060970C28A56425A4@EXCHANGESERVER> (raw)

>From: Uros Bizjak [ mailto:uros@kss-loka.si ]
>Sent: 11 January 2001 10:11

>  This code produces a strange result on Solaris 2.5.1 with gcc 2.8.1:
>
>--cut here--
>
>#include <stdio.h>
>int main()
>{
>        int x = 0;
>        int y = 5;
>        int z = 15;
>
>        printf ("%i, %i, %i, %i\n", x,  x += y, x += 2, x += z); 
>        return 0;
>}
>
>--cut here--
>
>  It displays "22, 5, 7, 22".
>  I would expect the result to be "0, 5, 7, 22".

  The code isn't valid.  It is part of the language specification that you
cannot rely on the order of evaluation of arguments to a function call, nor
can you rely on when the side effects take place.  There's a technical
thing about these things called sequence points and how you're only allowed
to change the value of a variable once between them, but that's quite 
in-depth.  If you called 

   printf("%d %d %d\n", x++, x++, x++);

the compiler is free to pass the same value for all three occurrences of x
and add three to it after the function call, or to evaluate the arguments
from left to right and pass 0, 1, 2, or from right to left and pass 2, 1, 0,
or indeed (since the language spec says this is undefined behaviour) to 
output a program that plays space invaders when you run it.  Steer clear
of such ambiguous constructs and you'll be ok.

      DaveK
-- 
The Boulder Pledge: "Under no circumstances will I ever purchase anything 
offered to me as the result of an unsolicited email message. Nor will I 
forward chain letters, petitions, mass mailings, or virus warnings to large 
numbers of others. This is my contribution to the survival of the online
community." 


**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

This footnote also confirms that this email message has been swept by
MIMEsweeper for the presence of computer viruses.

www.mimesweeper.com
**********************************************************************

             reply	other threads:[~2001-01-11  3:04 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-01-11  3:04 David Korn [this message]
  -- strict thread matches above, loose matches on Subject: below --
2008-06-16 13:01 Is this a GCC bug? Bingfeng Mei
2008-06-16 13:33 ` Bingfeng Mei
2001-01-17 23:30 Is this a gcc bug? Axel Kittenberger
2001-01-16 17:06 dewar
2001-01-14 16:14 dewar
2001-01-14 16:53 ` Dave Korn
2001-01-14  5:31 dewar
2001-01-14 15:51 ` Geoff Keating
2001-01-12 19:15 dewar
2001-01-12 19:29 ` Fergus Henderson
2001-01-12 19:14 dewar
2001-01-12 19:36 ` Alexandre Oliva
2001-01-12 18:21 dewar
2001-01-12 18:29 ` Joseph S. Myers
2001-01-12 18:49 ` Alexandre Oliva
2001-01-12 18:58 ` Fergus Henderson
2001-01-13 10:34 ` James Dennett
2001-01-16 17:01 ` Michael Eager
2001-01-17  3:21   ` Falk Hueffner
2001-01-17 14:57     ` Michael Eager
2001-01-12  1:18 Axel Kittenberger
2001-01-12 18:14 ` Alexandre Oliva
2001-01-12 19:11   ` Fergus Henderson
2001-01-13  3:21   ` Richard Earnshaw
2001-01-11 10:38 dewar
2001-01-11 10:30 dewar
2001-01-11 10:05 dewar
2001-01-11 10:00 David Korn
2001-01-11 21:36 ` Andy Walker
2001-01-13 18:45   ` Joern Rennecke
2001-01-14  2:21     ` Geoff Keating
2001-01-11  7:07 dewar
2001-01-11  5:53 dewar
2001-01-11  6:06 ` Bernd Schmidt
2001-01-11  8:40   ` Per Bothner
2001-01-11  9:03     ` Richard Earnshaw
2001-01-11  9:27       ` Alexandre Oliva
2001-01-11  9:33       ` Joe Buck
2001-01-11  9:38         ` Bernd Schmidt
2001-01-11  9:44           ` Richard Earnshaw
2001-01-11 10:57     ` Neil Booth
2001-01-11  5:49 dewar
2001-01-11  2:11 Uros Bizjak
2001-01-11  3:04 ` Alexandre Oliva

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=718D38CAB6E0D011B2C90060970C28A56425A4@EXCHANGESERVER \
    --to=dkorn@pixelpower.com \
    --cc=gcc@gcc.gnu.org \
    --cc=uros@kss-loka.si \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).