Problem determining the offset of a parent in a child.

Dirk Zoller
Sun Jun 4 10:22:00 GMT 2000


I have to work with lots of data structures. To make life easier,
I define structures describing those other structures. One interesting
property is, where in a child the parent is. I used a simple macro

// Return the distance in bytes of the parent P to beginning of
// derived type T. Where is the parent located inside the child?

# define PARENT_OFFSET(T,P) ((size_t)((P *)((T *)0x100)) - 0x100)

This works with most compilers including gcc-2.95. It fails in the
example I send as attachment with (and only with) all gcc snapshots I
tried after 2.95.

I modified the macro above in all ways I could imagine, ending
in a template function which is much clearer and looks very correct
and legal to me.

No matter what I do, current gcc snapshots keep giving me:

	/var/obj/model/data/core-records.ii:4376: non-lvalue in unary `&'

(Note that there is nowhere any unary `&' around)

I tried to find a simpler example but found that simple examples work
as expected:

template <class T, class P>
inline unsigned
parent_offset ()
  T *p = (T *)0x100;
  P *q = p;
  return (char *)q - (char *)p;

struct A {
  int a;

struct B : public A {
  int b;

int const c = parent_offset<B,A> ();

I'm using FreeBSD-4.0-STABLE, compiling like this:

europa:~$ c++ -v -xc++ -S core-records.ii
Reading specs from /opt/gnu/egcs-20000530/lib/gcc-lib/i386-unknown-freebsdelf4.0
gcc version 2.96 20000530 (experimental)
 /opt/gnu/egcs-20000530/lib/gcc-lib/i386-unknown-freebsdelf4.0/2.96/cpp -lang-c+
+ -v -iprefix /opt/gnu/bin/../lib/gcc-lib/i386-unknown-freebsdelf4.0/2.96/ -D__G
NUC__=2 -D__GNUG__=2 -D__GNUC_MINOR__=96 -D__GNUC_PATCHLEVEL__=0 -D__cplusplus -
Dunix -D__ELF__ -D__FreeBSD__ -D__unix__ -D__ELF__ -D__FreeBSD__ -D__unix -Asyst
em(unix) -Asystem(bsd) -Asystem(FreeBSD) -D__EXCEPTIONS -Acpu(i386) -Amachine(i3
86) -Di386 -D__i386 -D__i386__ -D__tune_i386__ core-records.ii /var/tmp/ccq36871
GNU CPP version 2.96 20000530 (experimental) (cpplib)
 (i386 FreeBSD/ELF)
ignoring nonexistent directory `/opt/gnu/include/g++-3'
ignoring nonexistent directory `/opt/gnu/lib/gcc-lib/i386-unknown-freebsdelf4.0/
ignoring nonexistent directory `/opt/gnu/i386-unknown-freebsdelf4.0/include'
#include "..." search starts here:
#include <...> search starts here:
End of search list.
 /opt/gnu/egcs-20000530/lib/gcc-lib/i386-unknown-freebsdelf4.0/2.96/cc1plus /var
/tmp/ccq36871.ii -quiet -dumpbase -version -o core-records.s
GNU C++ version 2.96 20000530 (experimental) (i386-unknown-freebsdelf4.0) compil
ed by GNU C version 2.96 20000530 (experimental).
core-records.ii: In function `void
__static_initialization_and_destruction_0 (int, int)':
core-records.ii:4376: non-lvalue in unary `&'
core-records.ii:4381: non-lvalue in unary `&'
core-records.ii:4386: non-lvalue in unary `&'
core-records.ii:4391: non-lvalue in unary `&'


Kind regards

Dirk Zoller				Fon: 06106-876566
Obere Marktstraße 5			e-mail:
63110 Rodgau

-------------- next part --------------
A non-text attachment was scrubbed...
Name: core-records.ii.bz2
Type: application/x-bzip2
Size: 15824 bytes
Desc: not available
URL: <>

More information about the Gcc-bugs mailing list