Topic: Storage layout compatibility with C


Author: russvz@wwa.com (Russell Van Zandt)
Date: 1996/09/05
Raw View

The commentary on the 1st part of section 9 in the ARM says:

"The layout of structures that are both C and C++ is identical,
so such structures can be shared freely between C and C++
programs without space or time overheads caused by
conversion or translation."

Shouldn't this be explicitly stated in the standard?  I cannot
find it anywhere in the draft standard.  Instead, the commentary
quoted above is replaced with a definition of "POD-structs",
but very little is ever said about the properties of POD structs,
other than that a pointer to POD struct corresponds to a
pointer to the first field therein.

The ARM then goes on to say:
"Even for classes using features not available to C, the
correspondence between the C++ class layout and an
equivalent C structure layout is usually obvious so that sharing
can be trivially achieved."

Section 10.1c goes on to say that for:
class A {
public:
   int a;
   void f(int i);
};
"No information is stored in an A except the integer a specified by
the user; in particular, no information about the member function
f() is stored in the object."

I can find nothing about this topic at all in the draft standard.
This whole issue of storage layout is important for object
persistence, and comes up again and again in programming
magazines.  Without a standard to back them up, the solutions
described in those articles, elegant though they may be, end
up being compiler specific, and could blow apart with a new
release of the compiler.

Russell Van Zandt
russvz@wwa.com


[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: clamage@taumet.Eng.Sun.COM (Steve Clamage)
Date: 1996/09/06
Raw View
In article 7lv@kirin.wwa.com, russvz@wwa.com (Russell Van Zandt) writes:
>
>The commentary on the 1st part of section 9 in the ARM says:
>
>"The layout of structures that are both C and C++ is identical,
>so such structures can be shared freely between C and C++
>programs without space or time overheads caused by
>conversion or translation."
>
>Shouldn't this be explicitly stated in the standard?  I cannot
>find it anywhere in the draft standard.  Instead, the commentary
>quoted above is replaced with a definition of "POD-structs",
>but very little is ever said about the properties of POD structs,
>other than that a pointer to POD struct corresponds to a
>pointer to the first field therein.

Section 9.2 "Class members" describes layout-compatibility of PODs.
If you look at all the rules, they turn out to be the same as the
C rules.

It is certainly the intent for corresponding data types in C and
C++ to behave identically, and for POD-structs to be layout-
compatible with the corresponding C struct. The problem is how
to say that in the C++ standard.

For example, is a C++ implementation required to include a C
implementation as an integral part? We have resisted such a
requirement, and I personally don't think that would be a
good requirement. C++ should stand alone, and in fact is
intended to be usable for all purposes where you don't
need assembler. If we include compatibility requirements with
C, shouldn't we also mandate compatibility with other languages
as well?

Absent a requirement that C++ includes C, how would the C++ standard
say that a C++ implementation must be compatible with C? It is
common to find mutually incompatible C implementations on the same
platform. What should C compatibility mean in that case? And
if the C++ implementation corresponds exactly to some C
implementation, and that C implementation changes, does that
suddenly make the C++ implementation non-conforming? I think
this a can of worms we don't want to open.

>The ARM then goes on to say:
>"Even for classes using features not available to C, the
>correspondence between the C++ class layout and an
>equivalent C structure layout is usually obvious so that sharing
>can be trivially achieved."
>
>Section 10.1c goes on to say that for:
>class A {
>public:
>   int a;
>   void f(int i);
>};
>"No information is stored in an A except the integer a specified by
>the user; in particular, no information about the member function
>f() is stored in the object."
>
>I can find nothing about this topic at all in the draft standard.

Your class example is a "POD-struct" by the definition in section 9, Classes.

---
Steve Clamage, stephen.clamage@eng.sun.com
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]