Why is this an issue?
offsetof
can lead to undefined behavior when the argument types are incompatible or when bit fields are used. Therefore
offsetof
should be avoided.
Noncompliant code example
#include <stddef.h>
struct A
{
int32_t i;
};
void f1 ( )
{
offsetof ( A, i ); // Noncompliant
}
Resources
- MISRA C:2004, 20.6 - The macro offsetof, in library <stddef.h>, shall not be used.
- MISRA C++ 2008, 18-2-1 - The macro offsetof, in library <stddef.h>, shall not be used.