On Tue, May 23, 2006 at 09:55:46PM +0200, Michael Kerrisk wrote:
> Justin,
> 
> > Included is a manpage documenting the offsetof() macro; please
> > consider including it.
> 
> Excellent idea!  Thanks for the page.  It will go in 2.34.
It was my idea .. I guess I found it in undocumented.3.

> >     /* Output is compiler-dependant */
> 
> Please put your pages through a spell checker.  (A fault that
> I'm also sometimes guilty of...)
I know; I saw this last night.  I'm building a spell check patch for
you.  manpages are actually quite good at spelling :)

BTW:
$ grep -xi ^dependant /usr/share/dict/words 
dependant

> >         printf("%d\n", offsetof(struct a, c));
> >         exit(EXIT_SUCCESS);
> > }
> > .fi
> > .SH NOTES
> > \fBoffsetof\fP can be implemented as:
> > .sp
> > .nf
> >         \fB#define offsetof(\fItype\fP, \fPmember\fP) \\
> >             \fB&((type *)0)->\fImember\fP - (char *)((\fPtype\fP *)0)
> > .fi
> 
> I'm inclined  to leave the above out (it could vary on some systems).  
> But anyway, why do you write the above when glibc says:
> 
> #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
Actually, I have:

  #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)

And in gcc-3.4 it is even uglier:

/* The cast to "char &" below avoids problems with user-defined
   "operator &", which can appear in a POD type.  */
#define offsetof(TYPE, MEMBER)                                  \
  (__offsetof__ (reinterpret_cast <size_t>                      \
                 (&reinterpret_cast <const volatile char &>     \
                  (static_cast<TYPE *> (0)->MEMBER))))

But I don't know why it would be necessary to subtract the casted zero
:)  At one point I had quite an amusing conversation about this.

I wrote it myself so I wasn't copying libc (although my source was gcc
anyway) and to make sure I still understood it.  Do you have a glibc
source, too?

Justin


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]

Reply via email to