Topic: Is this ANSI standard conforming


Author: rfg@netcom.com (Ronald F. Guilmette)
Date: Sun, 30 Oct 1994 07:49:51 GMT
Raw View
In article <384ch4$pc2@fsgm01.fnal.gov> b91926@fsgm01.fnal.gov (David Sachs) writes:
>Does anyone know if the following usage conforms to
>the ANSI C standard. The behavior in question is
>found in a major UNIX subsystem.
>
> A union contains the following members:
>    a)  int type;
>    b)  several struct s
>Each of the struct members of the union, has "int type;"
>as its first member.
>
>When this union is used, type contains a code, that denotes
>which of the structs the union actually contains.
>
>This behavior appears to work with every C abd C++ compiler
>I have tried, but IS THIS USAGE STANDARD CONFORMING?. Is it
>legal to write a C compiler, in which "int type" does not
>align for all members?

Why are you asking this question in comp.std.c++ rather than in comp.std.c?

--

-- Ron Guilmette, Sunnyvale, CA ---------- RG Consulting -------------------
---- E-mail: rfg@rahul.net ----------------- Purveyors of Compiler Test ----
-------------------------------------------- Suites and Bullet-Proof Shoes -




Author: b91926@fsgm01.fnal.gov (David Sachs)
Date: 31 Oct 1994 14:07:52 -0600
Raw View
rfg@netcom.com (Ronald F. Guilmette) writes:

>In article <384ch4$pc2@fsgm01.fnal.gov> b91926@fsgm01.fnal.gov (David Sachs) writes:
>>Does anyone know if the following usage conforms to
>>the ANSI C standard. The behavior in question is
>>found in a major UNIX subsystem.
>>
>> A union contains the following members:
>>    a)  int type;
>>    b)  several struct s
>>Each of the struct members of the union, has "int type;"
>>as its first member.
>>
>>When this union is used, type contains a code, that denotes
>>which of the structs the union actually contains.
>>
>>This behavior appears to work with every C abd C++ compiler
>>I have tried, but IS THIS USAGE STANDARD CONFORMING?. Is it
>>legal to write a C compiler, in which "int type" does not
>>align for all members?

>Why are you asking this question in comp.std.c++ rather than in comp.std.c?

>--

>-- Ron Guilmette, Sunnyvale, CA ---------- RG Consulting -------------------
>---- E-mail: rfg@rahul.net ----------------- Purveyors of Compiler Test ----
>-------------------------------------------- Suites and Bullet-Proof Shoes -

Because C++ is not a strict superset of C, and I am really interested
in the standard conformance of this usage for both languages.

The union question "works" with every compiler I have tried. In ANSI
standard C it is legal to have a union all of whose members are
structures with the same first members, but the use of "int type"
as a member of such a union is questionable (according to postings
in comp.std.c).

The Unix X-Windows code depends on the structure I mentioned.




Author: b91926@fsgm01.fnal.gov (David Sachs)
Date: 19 Oct 1994 19:10:12 -0500
Raw View
Does anyone know if the following usage conforms to
the ANSI C standard. The behavior in question is
found in a major UNIX subsystem.

 A union contains the following members:
    a)  int type;
    b)  several struct s
Each of the struct members of the union, has "int type;"
as its first member.

When this union is used, type contains a code, that denotes
which of the structs the union actually contains.

This behavior appears to work with every C abd C++ compiler
I have tried, but IS THIS USAGE STANDARD CONFORMING?. Is it
legal to write a C compiler, in which "int type" does not
align for all members?




Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 20 Oct 1994 15:51:41 GMT
Raw View
In article pc2@fsgm01.fnal.gov, b91926@fsgm01.fnal.gov (David Sachs) writes:
>Does anyone know if the following usage conforms to
>the ANSI C standard.

Why are you asking in a C++ group? :-)

> A union contains the following members:
>    a)  int type;
>    b)  several struct s
>Each of the struct members of the union, has "int type;"
>as its first member.
>
>When this union is used, type contains a code, that denotes
>which of the structs the union actually contains. ...
>IS THIS USAGE STANDARD CONFORMING?.

Yes.

>Is it legal to write a C compiler, in which "int type" does not
>align for all members?

No.

If in a union of structs each struct has the same initial sequence
of member types, each corresponding member must have the same address.
Further, each member of a union must start at the same address. Therefore,
the lone int must overlay the initial int of each struct.

The guarantees in C++ are the same, as long as the union and structs
are "Plain Old Data Structures", i.e., acceptable to a C compiler.
---
Steve Clamage, stephen.clamage@eng.sun.com