[net.lang.c] Another bug in C compilers

lvc@danews.UUCP (Larry Cipriani) (07/29/86)

I believe I've found a bug in the C compiler for the Sys 5 Rel 2 3B20.
Say we have a structure declared as:

struct ABC
{
	stuff
};

and then follow it with:

struct ABC;

I don't get any error message.  This is also in my CB-UNIX 2.3
pdp 11/70 C compiler.  Perhaps it is in your compiler also.  Is
this really a bug or am I missing something ?  If its not a bug
what on earth is it for ?
-- 

Larry Cipriani		AT&T Network Systems
danews!lvc		"Nothing is worse than an itch you can never scratch."

ark@alice.UucP (Andrew Koenig) (07/31/86)

> Say we have a structure declared as:
>
> struct ABC
> {
>	stuff
> };
>
> and then follow it with:
> 
> struct ABC;
> 
> I don't get any error message.

You are declaring an empty list of variables.  You can say:

	struct ABC u1, u2, u3;
	struct ABC v1, v2;
	struct ABC w1;
	struct ABC ;

They are all legitimate.

rgenter@BBN-LABS-B.ARPA (Rick Genter) (07/31/86)

     Accepting the declaration:

	struct	ABC;

is not a bug, it's a feature.  This is termed a *vacuous declaration*, and
permits the definition of cyclic recursive structures in an inner scope where
one of the structures renames a structure from on outer scope.  For example,

	struct	ABC	{
		int	foo;
		double	mumble;
	};

	main ()
	{
		struct	ABC;
		struct	DEF	{
			char	bar[ 128 ];
			int	bletch;
			struct	ABC	*foop;
		} a;

		struct	ABC	{
			double	giant[ 5000 ];
			struct	DEF	*barp;
		} b;

		<the rest of main>
	}

If you didn't have the vacuous declaration, the compiler would think that
"foop" pointed at a structure consisting of "int foo" and "double mumble".
Referencing foop->giant would get you "illegal structure pointer combination".
--------
Rick Genter 				BBN Laboratories Inc.
(617) 497-3848				10 Moulton St.  6/512
rgenter@labs-b.bbn.COM  (Internet new)	Cambridge, MA   02238
rgenter@bbn-labs-b.ARPA (Internet old)	linus!rgenter%BBN-LABS-B.ARPA (UUCP)

CHUBBY CHECKER just had a CHICKEN SANDWICH in downtown DULUTH!

brunner@sdsioa.UUCP (Rob Brunner X2830) (08/01/86)

> I believe I've found a bug in the C compiler for the Sys 5 Rel 2 3B20.
> Say we have a structure declared as:
> struct ABC
> {
> 	stuff
> };
> and then follow it with:
> struct ABC;
> ... no error ....
Which is equivalent to using:
int ;
as far as basic parsing is concerned.

This is NOT A BUG, but a feature...  If you look in K&R on pages 215,216
(in section 18.2-Declarations) you'll see essentially the following:

declaration:
     decl-specs init-decl-list[opt] ;    /* init-decl-list left out above */
...					 /* this is the key... you normally */
					 /* use something like 'int k;' this */
					 /* allows 'int ;'   */
decl-specs:
     type-spec decl-specs[opt] ;     /* decl-specs left out above */
...
type-spec:
     ...
     struct-or-union-spec
     ...
struct-or-union-spec:
     ...
     struct identifier
     ...

Why this would ever be done is beyond me.  Anyone have any applications
of this (and want to admit it)?  It might not be useful, but it is 
certainly legal.
-- 
Rob Brunner                              email:
Scripps Institution of Oceanography      brunner@sdsioa.UUCP
Mail Code A-010, UC San Diego            sdsioa!brunner@sdcsvax
San Diego, CA 92093                      {backbone}!sdcsvax!sdsioa!brunner
Phone: (619) 534-2040 (work)             (619) 452-7656 (home)

stephen@datacube.UUCP (08/06/86)

> I believe I've found a bug in the C compiler for the Sys 5 Rel 2 3B20.
> Say we have a structure declared as:
> struct ABC
> {
> 	stuff
> };
> and then follow it with:
> struct ABC;
> ... no error ....

It is necessary for structures which reference each other, as in:

struct a;

struct b {
    ...
    struct a *a_ptr;
};

struct a {
    ...
    struct b *b_ptr;
};

Stephen Watkins                    UUCP: ihnp4!datacube!stephen
Datacube Inc.; 4 Dearborn Rd.; Peabody, Ma. 01960; 617-535-6644