offsetof (was [m]allocation question)

Stephen Clamage steve at taumet.com
Thu Oct 4 01:52:53 AEST 1990


peter at objy.objy.com (Peter Moore) writes:

>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.

But offsetof is supplied by the compiler implementor, and whatever is 
supplied must work with the implementation.  There is no guarantee
that vendor A's offsetof will work with vendor B's compiler.  That is
why it is part of the standard header supplied with the compiler.
You simply #include <stddef.h> and use offsetof.  Your code is
guaranteed to work with any conforming implementation.  There is no
guarantee that YOU can write a portable offsetof.
-- 

Steve Clamage, TauMetric Corp, steve at taumet.com



More information about the Comp.std.c mailing list