Article View: comp.sys.atari.st
Article #26916Megamax bug or crappy C ?
From: tulp
Date: Wed, 20 May 1987 08:36
Date: Wed, 20 May 1987 08:36
50 lines
2341 bytes
2341 bytes
Try this piece of code with the Megamax C compiler. I am not sure whether
this is a bug (and if so I don't remember if it has been reported yet or
if it was fixed in a newer release; I use Version 1.1), maybe this is just
an example of bad C programming. I also tried this test with the Alcyon
(DRI) C compiler and this compiler did translate it the way I thought it
should be.
---
#include "stdio.h"
#include "osbind.h"
main()
{ int array[7];
register int i;
array[3] = 10;
i = 3;
while (i > 0)
array[i - 1] = array[i--] - 1; /* Trouble here. */
i = 3;
while (i < 6)
array[i + 1] = array[i++] + 1; /* And trouble here. */
for (i = 0;i< 7;i++)
printf("Array index %d is %d.\n",i,array[i]);
Cconin();
}
---
I looked at the code the compiler brewed and it appeared that first it
computes array[i--] - 1, including the post decrement, and then (with i
already decremented !) it computes array[i - 1]. So array[i - 1], really
becomes array[i - 2] ! As far as I know the post decrement should take
place after the entire expression is evaluated (the Alcyon compiler does
that). But I am not absolutely sure whether K & R is unambiguously clear
about this (I thought it said that the decrement takes place after the
assignment is done, but I don't have K & R laying around here, sorry).
Anyone ?
Of course I know that the expression could much easier be written as a for
loop. But this phenomenon occurred in a different context and I tried to
make a simple and clear test.
Drive and Megamax carefully,
Eduard Tulp tulp@cs.vu.nl
--------------------------------------------------------------------------
Live every day as if it were your last,
One day you will be right.
Technical Details
Message-ID:
<756@klipper.cs.vu.nl>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!water!watmath!clyde!rutgers!seismo!mcvax!botter!klipper!tulpShow Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: arc1-ca.pugleaf.net!utzoo!utgpu!water!watmath!clyde!rutgers!seismo!mcvax!botter!klipper!tulp From: tulp@klipper.UUCP Newsgroups: comp.sys.atari.st Subject: Megamax bug or crappy C ? Message-ID: <756@klipper.cs.vu.nl> Date: Wed, 20-May-87 04:36:58 EDT Article-I.D.: klipper.756 Posted: Wed May 20 04:36:58 1987 Date-Received: Thu, 21-May-87 07:10:27 EDT Reply-To: tulp@cs.vu.nl (Tulp E) Organization: VU Informatica, Amsterdam Lines: 50