offsetof (was [m]allocation question)

Peter Moore peter at objy.objy.com
Mon Oct 1 12:23:25 AEST 1990


The validity of sizeof((type *)->field)) is especially interesting given
the sample (though carefully described as unportable) definitions of offsetof
in the Rationale (4.1.5: pg 75):

#define offsetof(type, field) (size_t)&(((type*)0)->field)

This isn't going to work if sizeof((type *)->field) doesn't work.

By the way, the above definition of offsetof always seemed like a bad one,
since it makes the unnecessary assumption that the null pointer has a 0
bit-pattern.  Now practically, that is probably true for almost all compilers,
if only because there is so much code that implicitly depends on it.  But
wouldn't a more politically correct example be:

#define offset(type, field) \
    ( ((char *) &(((type *)0)->field)) - ((char *) 0) )


	Peter Moore
	peter at objy.com



More information about the Comp.std.c mailing list