Re: Mechanics of calculating structure-member offsets
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Sat, 03 Mar 2007 02:07:45 -0800
Sensei <senseiwa at Apple's mac dot com> writes:
On 2007-03-02 22:08:47 +0100, Keith Thompson <kst-u@xxxxxxx> said:
And if your implementation doesn't provide offsetof() (it's defined in
<stddef.h>), find a different one. The standard requires offsetof()
to exist; if it doesn't, your implementation is *badly* broken, and
anything else in the language could be missing or broken.
Just a curiosity: is offsetof() required by the standard to be a
#define?
Yes.
Can I check against it by simply using the following code?
#ifndef offsetof
#error .........
#endif
Well, probably. In a conforming implementation, that will just tell
you whether you've #include'd <stddef.h> (and not explicitly undefined
offsetof afterward). In a non-conforming implementation, how do you
know that #ifndef is going to work properly?
It's possible to have a working compiler and a broken <stddef.h>, but
I really don't think there's much point in testing for it (unless
you're writing a conformance test suite). If you need offsetof(),
just go ahead and use it; if it's not defined, you'll get an error
message. (The compiler won't treat it as a call to an external
function because the first argument is a type name.)
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.
- References:
- Mechanics of calculating structure-member offsets
- From: cman
- Re: Mechanics of calculating structure-member offsets
- From: user923005
- Re: Mechanics of calculating structure-member offsets
- From: Keith Thompson
- Re: Mechanics of calculating structure-member offsets
- From: Sensei
- Mechanics of calculating structure-member offsets
- Prev by Date: Re: where does the value returned by main go ?
- Next by Date: Re: what will be the value of #define
- Previous by thread: Re: Mechanics of calculating structure-member offsets
- Next by thread: Re: Mechanics of calculating structure-member offsets
- Index(es):
Relevant Pages
|
|