Re: offsetof




Flash Gordon wrote:
> junky_fellow@xxxxxxxxxxx wrote:
> > luke wrote:
> >
> >>hi all,
> >>i have another question.
> >>I've read the FAQ regarding my former question (sizeof struct and
> >>union) and in question 2.14 it talks about offset macro.
>
> This is good. Not just your reading the FAQ, but also telling us you
> have read the relevant section. I wish more people would do as you have
> done and read the FAQ and then tell us.
>
> >>#define offsetof(type, mem) ((size_t) \
> >> ((char *)&((type *)0)->mem - (char *)(type *)0))
> >>
> >>Can anyone explain me how it works.
> >>1)I can't understand what "0" casted to (type *) means
> >>2)Isn't the second menber of substraction "(char *)(type *)0" useless?
> >
> > The second member of sustraction would be required on implementation
> > where the NULL pointer is *not* represented by all-bits-zero.
>
> That is part of it. It also avoids the issue of how pointers are
> converted to integers. For example, I believe that on the Cray
> implementation of C the offset of a byte within a 64 bit word is
> actually stored in the high bits of a char pointer. Pointer subtraction
> obviously has to allow for this, but converting a pointer to an integer
> does not (although it could).
>
> On some system it may well be defined as the far simpler
> #define offsetof(type, mem) ((size_t)&((type *)0)->mem)
> because the implementer has chosen to do things in such a way that this
> works.
>
> > Probably, this is why offsetof macro is implementation specific.
>
> Yes, the way the offsetof macro is implemented is implementation
> specific because for some implementations it needs to use strange tricks
> to work (on all implementation is has to use some tricks, but not always
> strange ones). Obviously the author of the implementation can make any
> strange trick they want work and then use it, but someone using the
> implementation can't use such tricks because they either invoke
> undefined behaviour or implementation defined behaviour and so could do
> anything on another system.
>
> If you (or luke) looks in the system headers for your implementation you
> will find lots of other non-portable tricks being used, which is why it
> is not generally worth looking in the system headers to see how things
> are done.
>
> This is also why you should use the facilities provided by the
> implementation rather than trying to roll your own.

Thanx Flash for the nice explanation. I have few more questions to ask.
1) K&R A.7.7
If two pointers to objects of the same type are subtracted
the result is a signed integral value
representing the displacement between the pointed-to objects.
The value is undefined unless the pointers point to objects within
the
same array.
In the offsetof macro, is the substraction defined ?

2) If this is defined then the macro,
#define offsetof(type, mem) ((size_t) \
((char *)&((type *)0)->mem - (char *)(type *)0))

should work on all implementations ? So, why should we use
implementation
specific offsetof macro ?

.



Relevant Pages

  • Re: Holding different classes.
    ... a single type. ... pointers to objects that belong to a single class hierarchy. ... a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq ...
    (alt.comp.lang.learn.c-cpp)
  • Re: pointers and files
    ... I'm wrk prj C. ... > address as that of the first char. ... pointers to FILE ... C++ Faq: http://www.parashift.com/c++-faq-lite ...
    (comp.lang.c)
  • Re: [OT] K&R Wishlist
    ... case of pointers, you can easily end up using the address of an array ... (And read the rest of the FAQ while you're ... understanding of the language. ... system-specific newsgroup. ...
    (comp.lang.c)
  • Re: A few things remain unclear...
    ... > understand(Feel free to direct me to a tutorial or a FAQ ... In a struct each field has its own storage area. ... to literal should be pointers to constant data. ... should be "char *". ...
    (comp.lang.c)
  • Re: offsetof
    ... >Flash Gordon wrote: ... > The value is undefined unless the pointers point to objects within ... > In the offsetof macro, ... license to do pointer subtractions like this. ...
    (comp.lang.c)