On Wed, Jun 30, 2004 at 12:04:03 +0300, Nir Tzachar wrote: > hi > > > > to get the offset of member c in this struct, we need to size of all > > > members which come b4 c: offset_of_c = sizeof(a)+sizeof(b) . > > no, this is not correct. the compilers are free to add padding to meet > > alignment requirements of a given platform. > > you are correct, maybe i oversimplified a bit.... > > > > however, a more generic and _much_ better way: > > > lets say you had a pointer to a struct foo (foo_ptr), so you can get the > > > offset like this: > > > > > > offset_of_c = &foo_ptr->c - foo_ptr > > > > > > the macro actually saves the subtraction, by letting the compiler think > > > that foo_ptr is 0 ((TYPE *)0) (located at start of memory). > > the assumption here is that null is repersented as a bunch of zeros on > > a platform. if that is not the case, you will get strange results. > > i dont see your point. this has nothing to do with the NULL pointer, but > with a pointer whose base address is 0, which saves the subtraction... > as far as this macro concerns, NULL may be defined like this: > #define NULL ((void *)400) > > comments?? No, it may not! Numeric 0 must convert to NULL pointer and vice versa (if for nothing than for the fact, that NULL must be FALSE). Noone said, that the typecast does not add some code though, so the real memory representation may not be all zeroes. On the other hand, Linux kernel is a bit specific. It is NOT written in ANSI C nor ISO C nor any other standard, but in GNU C. As long as it works in gcc, it is safe for use. ------------------------------------------------------------------------------- Jan 'Bulb' Hudec <bulb@ucw.cz>
Attachment:
signature.asc
Description: Digital signature