tags:

views:

115

answers:

3

Having just seen http://stackoverflow.com/questions/3453063/what-does-the-following-macro-do I gotta ask my own question: why do so many applications' headers define offsetof themselves? Is there some reason why <stddef.h> is not to be relied upon?

+7  A: 

I don't think it's that they distrust the standard offsetof -- at least from what I've seen, it's usually that they're just unaware of it.

Jerry Coffin
It's sort of sad how plausible this is.
Zack
+3  A: 

Is there some reason why is not to be relied upon?

I know one of the reasons. GCC produces a warning when standard offsetof() is used on fields of C++ classes. That leads some people to roll out their own version which doesn't trigger the warning.

Dummy00001
Terrible idea. Warnings are there for a reason...That's like saying "Oh, undefined behavior? I spit on you...by the way why is my program non-portable and crashing?"
GMan
Which is dumb, because that warning is there for good reasons.
Zan Lynx
Yes. Especially on one of the projects I worked they actually were mixing polymorphism/multiple inheritance and the offsetof() and there were memory corruption because of that and it took me forever to explain the people why it couldn't work and why there is a warning for it.
Dummy00001
This is the programming equivalent of burying your head in the sand in hopes that you won't been seen and devoured.
Tyler McHenry
+2  A: 

Or maybe it's legacy code from a C compiler that wasn't ANSI compliant and didn't have offsetof?

tomlogic
I'd believe this except it seems like all that code should have died out by now. Maybe people have been cargo-culting it from each other ever since because they don't know about stddef.h, like Jerry suggests.
Zack
@Zack: I'd easily believe that people have been cargo culting it. There are a lot of misconceptions that `offsetof` wasn't standardized until C99 too.
jamesdlin