Topic: offsetof(class,member)?


Author: rich@kastle.com (Richard Krehbiel)
Date: Fri, 5 Aug 1994 15:44:40 GMT
Raw View
I know that class instances in C++ may have a few hidden items, like
virtual base class pointers and virtual function table pointers.
Given these strange and hidden things, I want to confirm the behavior
of "offsetof", that it is widely available, works, and is a defined
part of the C++ standard.

What I need is this:

 class A {
  // other stuff
  int member;
  // other stuff
 } iA;

 int i = offsetof(A, member);

Following this, (int *)((char *)&iA + i) points to iA.member, right?

 int *pmember = &iA.member;

I also need (A *)((char *)pmember - i) to give a fully functional
pointer to iA.

Does it matter if the data member is a class instance?

 class B {
  // other stuff
  class A member;
  // other stuff
 } iB;

 int j = offsetof(B, member);

 A *pA = (class A *)((char *)&B + j);

 B *pB = (class B *)((char *)pB - j);

Do pA and pB result in functional class pointers?

I already know that member functions aren't actually "members" in the
same sense that data members are, and that therefore offsetof(class,
memberfunction) is meaningless.

I need to be sure this works so that I can use some linked list
handling macros that have been written for C.  Thanks.




Author: rfg@netcom.com (Ronald F. Guilmette)
Date: Sun, 7 Aug 1994 08:01:39 GMT
Raw View
In article <RICH.94Aug05104440@standalone.kastle.com> rich@kastle.com (Richard Krehbiel) writes:
>I know that class instances in C++ may have a few hidden items, like
>virtual base class pointers and virtual function table pointers.
>Given these strange and hidden things, I want to confirm the behavior
>of "offsetof", that it is widely available, works, and is a defined
>part of the C++ standard.

Two points:

First, there is no C++ standard (yet).

Second, I think you would be making a miskate to assume that the offsetof
macro is (as of this moment) really very well defined in C++.

The current draft of the C++ standard says (in effect) that the definition
of (and description of) the ``offsetof'' macro is effectively included
``by reference'' into the C++ standard (from the C standard).

I note however that the C standard says that:

  offsetof (type, member-designator)

 expands to an integral constant expression that has type size_t...

There are several obvious problems here.

First, the C++ standard does not contain (or define) any such term as
``integral constant expression'', and there is no such notion currently
in C++.  (This is just another one of the many many unfortunate by-products
of the fact that no serious effort has ever been made to reconcile the
terminology of the draft C++ standard with that of the *existing* C
standard... which is supposedly a base document for the C++ standard).

Second and more importantly, if we take the description of ``offsetof''
verbatim from the C standard (as the current draft C++ standard seems
to tell us to do) then we would probably conclude that:

  offsetof (T, f)

yields some specific value of type size_t even in cases where `f' is a
*function member* or even a *type member* of the type T!

I hope that such obviously nonsensical conclusions will make it clear
that the description of the ``offsetof'' macro given in the C standard
cannot simply be included ``by reference'' (and without caveats) into
the C++ standard.  Unfortunately, at the moment, that seems to be exactly
what the draft C++ standard suggests that we (mentally) do.

--

-- Ron Guilmette, Sunnyvale, CA ---------- RG Consulting -------------------
---- domain addr: rfg@netcom.com ----------- Purveyors of Compiler Test ----
---- uucp addr: ...!uunet!netcom!rfg ------- Suites and Bullet-Proof Shoes -