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?
views:
115answers:
3
+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
2010-08-10 20:35:33
It's sort of sad how plausible this is.
Zack
2010-08-10 21:40:55
+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
2010-08-10 20:50:26
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
2010-08-10 21:01:54
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
2010-08-10 21:06:18
This is the programming equivalent of burying your head in the sand in hopes that you won't been seen and devoured.
Tyler McHenry
2010-08-10 21:12:30
+2
A:
Or maybe it's legacy code from a C compiler that wasn't ANSI compliant and didn't have offsetof
?
tomlogic
2010-08-10 21:08:58
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
2010-08-10 21:40:02
@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
2010-08-10 22:08:35