4 ms·
The author may be correct, which is of course the best kind of correct. Yet I have written code for more than 25 years that depends on the behavior exemplified
by dmethvin 11y ago
The author may be correct, which is of course the best kind of correct. Yet I have written code for more than 25 years that depends on the behavior exemplified in the `offsetof()` macro referenced there. When writing low-level code it's really handy to know the offset of a member in a struct.
- mpyne 11y agoGCC advances frequently break code exploiting undefined behavior that had been working in a certain way for years. "Works as you'd expect" is certainly one of the possibilities of undefined behavior, after all, but that's no guarantee the next compiler version will continue to work as you'd expect. In fact I'm pretty sure a GCC update broke this exact type of code in the Linux kernel, by effectively removing null pointer checks after the optimizer used its knowledge of a pointer use later in a function to assume that the pointer must be valid and non-null (after all, if it had been null, the behavior would be undefined, and the compiler is allowed to assume no undefined behavior happens, which means the earlier null pointer check had been redundant). I believe that got fixed in GCC to do what you'd expect, but that's just one example of the awesomeness of undefined behavior in C-based languages.
- kps 11y ago> When writing low-level code it's really handy to know > the offset of a member in a struct. C provides offsetof() in <stddef.h> for that purpose, precisely because it's not expressible in C without compiler support.
- madmoose 11y agoYour compiler probably has a built-in method to achieve the equivalent of offsetof without invoking undefined behavior. GCC has __builtin_offsetof: https://gcc.gnu.org/onlinedocs/gcc/Offsetof.html https://gcc.gnu.org/onlinedocs/gcc/Offsetof.html
- masklinn 11y agooffsetof is part of C89 stddef.h, why would you call the underlying GCC-specific __builtin_offsetof? Just call offsetof that's what it's here for, __builtin_offsetof is the GCC implementation detail.
- noselasd 11y agoSo have many people. But from my experience, it is not a fun day when you find out the new compiler version you upgraded to 3 months month ago have "miscompiled" your code for the last 2 releases of your codebase, since it contained undefined behavior which worked fine for the past 15 years when using older versions of the compiler.