[svlug] Structure Field Offset Measurement
J C Lawrence
claw at kanga.nu
Thu Jan 4 23:14:02 PST 2001
On Thu, 4 Jan 2001 18:29:48 -0800
Karen Shaeffer <shaeffer at best.com> wrote:
> #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
This assumes that address zero is of suitable alignment for any
structure. This is *NOT* always true.
> I fully understand what this accomplishes and am cognizant it
> works as advertised, but I am not aware of what rules provide for
> "((size_t) &((struct s *)0)->elem)" equating to a valid r-value.
Its just a cheat that works on most platforms. It is relying on
platform-defined behaviour rather than language defined behaviour,
to gen an r-type.
> Is this MACRO *defined* as part of the c standard?
offsetof() is defined and required, yes, but ANSI leaves the
implementation up to the vendor.
As a side note I've been recently playing with raw IO under Linux
2.2.17 (patched) and 2.4.0-test12 (haven't gotten it to work
actually, but that's another matter). The basie method for betting
a page alligned buffer in user psace by exploiting /dev/zero and
mmap() is a perfect example of replying on system-specific
implementation details. much like the macro above.
--
J C Lawrence claw at kanga.nu
---------(*) http://www.kanga.nu/~claw/
--=| A man is as sane as he is dangerous to his environment |=--
More information about the svlug
mailing list