greim@sbsvax.UUCP (Michael Greim) (08/03/89)
About 2 weeks I asked in <800@sbsvax.UUCP> whether a certain program was incorrect and whether C compilers which only gave me a warning were in error to do so, ie. is it permissible for a compiler only to warn about the error? Here is the program (just to refresh your memory): ---------- cut here ---------------------------- /* * What does the compiler do with this? */ # include <stdio.h> struct link { struct link * next; int count; }; int fix0; struct link * t; int fix1; main () { printf ("t lies at %1d, brk is at %1d\n", (int)(&t), (int)sbrk(0)); printf ("Addresses of %1d byte objects around t : %1d and %1d\n", sizeof(int), (int)(&fix0), (int)(&fix1)); t = NULL; t.count = 5; if (t.count == 5) printf ("Assignment worked.\n"); else printf ("Assignment did not work\n"); printf ("Value 5 has been put at address %1d\n", &(t.count)); } ---------- cut here ---------------------------- Output from VAX running 43bsd: Script started on Wed Jul 19 11:44:22 1989 % cc test78.c "test78.c", line 21: warning: struct/union or struct/union pointer required "test78.c", line 22: warning: struct/union or struct/union pointer required "test78.c", line 26: warning: struct/union or struct/union pointer required % a.out t lies at 6008, brk is at 6176 Addresses of 4 byte objects around t : 6004 and 6012 Assignment worked. Value 5 has been put at address 6012 % exit % script done on Wed Jul 19 11:44:47 1989 Obviously the compiler took the address of t as the base for computing the address of the member count and put the constant 5 there. Thanks to all who answered. Here are the results: Of course the program is incorrect. t is defined as a pointer not as a structure. (I knew that of course) As to the second (and most important question) "Is it permissible for a compiler only to warn about this?" I can only cite Chris Torek: >The program is not legal C, but compiling it (particularly with a warning) >is not wrong: at least, not according to the pANS. Most language standards >do not require that a compiler detect and complain about incorrect >programs; they say only what the compiler must do with correct programs. >(This is necessary in general since the variety of incorrect programs >seems to be infinite. :-) ) > >PCC is overly trusting of structure and union field references so as >to be backward compatible with Version 4 Unix. (The source for adb >depended on the ability to declare a double, and then use structure >field operators on that variable, until recently. Taking backward >compatibility hacks out of the compiler reveals a surprising amount >of bad software sometimes. . . .) In my opinion any compiler should give me an error for the program, but I can make do with the warning. A note to all who warned me about the usage of the variables fix0 and fix1: I know that I cannot rely on them being at any address relative to t. It was just a quick test to see where the constant was written to. The program could also have dumped core. -mg -- Michael Greim Email : greim@sbsvax.informatik.uni-saarland.dbp.de or : ...!uunet!unido!sbsvax!greim [.signature removed by the board of censors for electronic mail's main executive computer because it contained a four letter word ("word")]