offsetof (was: Re: Something new for C?)
Ray Butterworth
rbutterworth at watmath.waterloo.edu
Wed Nov 16 02:29:01 AEST 1988
In article <26740 at ucbvax.BERKELEY.EDU>, jas at ernie.Berkeley.EDU (Jim Shankland) writes:
> On such machines, you must have an instance of the structure:
> struct mumble foo;
> int mem_offset = (char *)(&foo.member) - (char *)(&foo);
> Sigh. The things you have to put up with in the portability trenches.
The same argument applies for the alignof macro:
#define alignof(type) /* assumes double is max aligned */ \
( (char*)&(((struct {double _d; char _c; type _t;}*)0)->_t) \
- (char*)&(((struct {double _d; char _c; type _t;}*)0)->_c) )
which is equally non-portable,
but for some reason the Committee chose not to make this macro
part of the Standard even though alignof() seems to be more useful
(to me at least) than offsetof().
> This is about the 10,000th reason that we need ANSI C *now*.
And this is one more reason why "ANSI C now" still isn't good enough.
More information about the Comp.lang.c
mailing list