A
Andrew Reilly
I just tried the following program (CodeWarrior 10 on MacOS X):
Same for gcc4 on MacOS X. However this slight permutation of your
program (only the comparison line has changed):
#include <stdio.h>
#define SIZE (50*1000000L)
typedef struct {
char a [SIZE];
} bigstruct;
static bigstruct bigarray [8];
int main(void)
{
printf("%lx\n", (unsigned long) &bigarray [0]);
printf("%lx\n", (unsigned long) &bigarray [9]);
printf("%lx\n", (unsigned long) &bigarray [-1]);
if (&bigarray [-1] - & bigarray [0] < 0)
printf ("Everything is fine\n");
else
printf ("The C Standard is right: &bigarray [-1] is broken\n");
return 0;
}
produces:
3080
1ad2a500
fd054000
Everything is fine
So what we see is that (a) pointer comparisons use direct unsigned integer
comparison, instead of checking the sign of the pointer difference---since
pointer comparisons only make sense in the context of an indivdual object,
I'd argue that the compiler is doing the wrong thing here, and the
comparison should instead have been done in the context of a pointer
difference; and (b) your printf string about "&bigarray[-1] is broken" is
wrong, since that's not what the code showed at all. What it showed is
that &bigarray[-1] could be formed, that &bigarray[0] was one element to
the right of it, and that hell did not freeze over (nor was any trap
taken), since you did not attempt to access any memory there.
Cheers,