*BSD News Article 82452


Return to BSD News archive

#! rnews 2279 bsd
Message-ID: <3280F04E.4D5A@cococo.net>
Date: Wed, 06 Nov 1996 20:08:46 +0000
From: Kelley <kelley@cococo.net>
Reply-To: kelley@cococo.net
X-Mailer: Mozilla 3.0Gold (Win95; I)
MIME-Version: 1.0
Newsgroups: comp.unix.bsd.freebsd.misc
Subject: Re: Compiler bug?
References: <55p7ck$r2l@csgrad.cs.vt.edu>
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: penguin.cococo.net
Lines: 50
Path: euryale.cc.adfa.oz.au!newshost.carno.net.au!harbinger.cc.monash.edu.au!munnari.OZ.AU!news.ecn.uoknor.edu!feed1.news.erols.com!howland.erols.net!newsfeed.internetmci.com!news.cococo.net!penguin.cococo.net

Tommy Johnson wrote:
> 
> This program doesn't work as I think it should.   On line 10, b is not
> incremented between the first and second b++'s, though it is correct
> after the entire expression.
> 
> On FreeBSD 2.2-Current as of about Oct 13, the program below produces
> 0x1212 instead of 0x3412  (gcc 2.7.2.1, or gcc 2.6.3).  It also happens
> on NetBSD 1.1B as of about May 16 on an HP300 (gcc 2.7.2).  It DOES produce
> 0x3412 on a DEC alpha with gcc 2.7.2, or gcc 2.7.2 on a DECstation
> (OSF1 3.2 148, and Ultrix 4.4 rev 69 respectively).
> 
> (It also breaks on FreeBSD 2.1.5-Release with gcc 2.6.3)
> 
> Am I hallucinating?   TIA...
> 
> -Tom "Bugger, now I have to wait for someone to wake up." -Marcus
> tjohnson@csgrad.cs.vt.edu  "My other computer ALSO runs unix." -me   <*>
> http://csgrad.cs.vt.edu/~tjohnson/  Commercial email is unwelcome.
> Message and signature (c) 1996 Tommy O. Johnson, all rights reserved
> 
> --- Bite Here --- Bite Here --- Bite Here ---
> 
> /* Copyright: test program, do with it as you wish
> ** Tommy Johnson
> */
> main()
> {
>         int a,b;
>         unsigned char c[2];
> 
>         b=0;
>         c[0]=0x12;
>         c[1]=0x34;
> 
>         a=c[b++]|((c[b++])<<8);
>         printf("should be 0x3412 %4x\n",a);
> }

Not sure if it is a bug or not, since the expression (to the best of my
recollection) with a post increment operator isn't evaluated until the
next expression, but try this out on line 10:

a=c[--b] | ((c[++b])<<8);

Very interesting since it is the same variable, but I can tell you that
the Borland C++ 4.5 (on Windoze95)compiler evaluates to 0x1212 also.

Later
Kelley