[comp.lang.c++] bug with unions?

satz@Shasta.STANFORD.EDU (Greg Satz) (08/04/87)

While trying to convert some code to use cfront I came across the
following anomoly.  When declaring a union with structs contained
within, if the struct definitions contain tags, the resulting union
generated is empty.  However if you remove the struct tags, the union
is generated with the structs correctly.  Following is the C++ source
and the generated C code.

This C++ code:

union ttype {
    struct na {
	unsigned short hint;
	unsigned short pint;
    } na;
    struct pl {
	unsigned char nint;
	unsigned char next;
	unsigned long n;
	unsigned char data[2];
    } pl;
    struct err {
	unsigned short reason;
	char copy[2];
    } err;
};

generates this C code:

# 1 ""

/* <<cfront 05/20/86>> */

# 1 ""
int *_new ();

# 1 ""
int _delete ();

# 1 ""
int *_vec_new ();

# 1 ""
int _vec_delete ();

# 1 ""
struct na { /* sizeof = 4 */

# 3 ""
unsigned short _na_hint ;
unsigned short _na_pint ;
} ;
struct pl { /* sizeof = 12 */
unsigned char _pl_nint ;
unsigned char _pl_next ;
unsigned long _pl_n ;
unsigned char _pl_data [2];
} ;
struct err { /* sizeof = 4 */
unsigned short _err_reason ;
char _err_copy [2];
} ;
union ttype { /* sizeof = 12 */		/** The size is correct **/
} ;					/** but the result is empty. **/

/* the end */

jss@hector..UUCP (Jerry Schwarz) (08/06/87)

In article <1910@Shasta.STANFORD.EDU> satz@Shasta.UUCP (Greg Satz) writes:
>
>While trying to convert some code to use cfront I came across the
>following anomoly. 
  ...
>union ttype {
>    struct na {
>	unsigned short hint;
>	unsigned short pint;
>    } na;
...
>};

One of the subtle differences between C and C++ is that tags in C
live in a different name space from ordinary (variable and function)
identifiers whereas in C++ they live in the same name space.  Thus
the meaning of

        struct s { ... } s ;

in C++ is not really defined by the C++ book.

Changing the tag to be different from the member name  also cures the
problem.

This is known as the "stat stat" problem around here because "stat"
is the one identifier defined in the UNIX C header files as both a
structure tag and an ordinary (in this case function) identifier.

Jerry Schwarz
Bell Labs
Murray Hill