Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 12 Oct 1996 12:50:02 +0930 (CST)
From:      Michael Smith <msmith@atrad.adelaide.edu.au>
To:        charnier@xp11.frmug.org (Philippe Charnier)
Cc:        current@FreeBSD.org
Subject:   Re: gcc's bug. please comment
Message-ID:  <199610120320.MAA03815@genesis.atrad.adelaide.edu.au>
In-Reply-To: <199610111905.VAA01615@xp11.frmug.org> from "Philippe Charnier" at Oct 11, 96 09:05:36 pm

next in thread | previous in thread | raw e-mail | index | archive | help
Philippe Charnier stands accused of saying:
> 
> A friend found a bug in gcc-2.7.2.1. The following program fails on
> i386 architecture (both FreeBSD and solaris-x86) and works as expected
> on sparc (both sunos and solaris2.5.1) and on hp (hp-ux9.07). As I'm
> not on the gcc-bug list, please comment before I report the bug. This
> afternoon I sent a bug report to Sun but it was before trying on
> FreeBSD.
> 
> The output should be `1 2' but it is `2 1' on i386 computers.
> Here is the code:
> 
> ----------mypb2.c-----------------
> #include <stdio.h>
> #include <stdlib.h>
> 
> main()
> { FILE *fic;
>   int a[2][50], i = 0;
> 
>   fic=fopen("mypb2.csv","r");
>   fscanf(fic, "%d;%d", &a[1][i++], &a[1][i++]);

Arguments to C functions are usually stacked right-to-left (this is
the only way to make functions with variable numbers of arguments work
sensibly)(1).  Thus, the args to fscanf are evaluated right-to-left, and
thus your code is working as one might expect.

I would imagine that the Sun and HP compilers calculate the a indices
before stacking them for the call, and thus evaluate left-to-right.
To me, this implies pedantic metrics-and-clockwatching compiler
design, or possibly one or more large customers with large legacy
codebases which depend upon code like the snippet above.

The C standard _explicitly_ forbids you from making any assumptions about
the order of evaluation of function arguments, so the bug is yours.

(1) Yes, I know there are other ways of doing it.  No, none of them work
    sensibly.
-- 
]] Mike Smith, Software Engineer        msmith@atrad.adelaide.edu.au    [[
]] Genesis Software                     genesis@atrad.adelaide.edu.au   [[
]] High-speed data acquisition and      (GSM mobile) 0411-222-496       [[
]] realtime instrument control          (ph/fax)  +61-8-267-3039        [[
]] Collector of old Unix hardware.      "Where are your PEZ?" The Tick  [[



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199610120320.MAA03815>