Re: offsetof



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.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
.




Relevant Pages

  • Re: Really **wonderful** website for C interview questions!!!!!
    ... > then you should take a look at the C FAQ by Steve Summit... ... to return a string from a function"), and compare FAQ 7.5b. ... ("What is a NULL pointer? ... Or see pages/7_6.html ("How to declare an array of N pointers to ...
    (comp.lang.c)
  • Re: working with addresses
    ... The URL to the C FAQ has been in my sig block for years. ... what the FAQ emphasises is that those who seek to write portable code ... reason for it, and I guessed that speed might be your reason. ... likely to make for further imbalances in pointer sizes. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: help alleaviate inheritance design confusion
    ... > within the bounds of Liskov Substitution Principle. ... > the base pointer to a derived object to garner access to the derived ... a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq ...
    (alt.comp.lang.learn.c-cpp)
  • Re: current address
    ... > Vu Pham wrote: ... >> In order to mark the current address at a given point of the variable ... The OP should also remember that although pointer can be written ... C++ Faq: http://www.parashift.com/c++-faq-lite ...
    (comp.lang.c)
  • Re: Hungarian notation in C++ (is it used or not today, and if so, how?)
    ... > p (pointer) ... > by (unsigned char, or byte) ... that the prefix is correct. ... C++ Faq: http://www.parashift.com/c++-faq-lite ...
    (alt.comp.lang.learn.c-cpp)