This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: C ABI: struct/union inconsistency




--On Friday, September 27, 2002 08:14:48 PM +0200 Gabriel Dos Reis <gdr@integrable-solutions.net> wrote:

Richard Henderson <rth@redhat.com> writes:

[...]

| > That means we lose C/C++ compatibility even for a type which is in
| > the common subset of both languages.
|
| Compatibility between C and C++ is interesting from a data sharing
| standpoint.  Empty classes contain no data to be shared, ergo are
| not interesting.
I don't understand this point.

What about source compatibility?

My point was that I would like a header file that looks like:

extern "C" {
 struct S {};
 ...
}

to have the same meaning in both languages; that's not possible if S has
size zero in C.  Gaby's example is one exmaple; sizeof(S) is of course
another.  Another is structs that include S as a data member, etc., etc.

The current behavior means that verifying that a C structure will have
the same layout in C++ requires a recursive walk of the entire data
structure.  Consider, for example:

 struct S {};
 struct T { struct S s1; struct S s2; };

In C, GCC gives T size zero; both size zero S structures at located at
offset zero.  In C++, the language requires that T have size at least
two.  There might be actual data mixed in with the empty stuff; in that
case, the offsets of the actual data will be different in C and C++.

How am I, as a C or C++ programmer writing header files to be used in both
languages supposed to deal with this issue?

What if some other library on the system (e.g., glibc) ends up with a
size zero structure in its headers?  Then, I cannot even know that I
have this problem without recursively examining all the types used in
my program?

--
Mark Mitchell                mark@codesourcery.com
CodeSourcery, LLC            http://www.codesourcery.com


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]