This rule is part of MISRA C++:2023.
Usage of this content is governed by Sonar’s terms and conditions. Redistribution is
prohibited.
Rule 21.2.4 - The macro offsetof shall not be used
[support.types.layout] Undefined 1
[C11] / 7.19; Undefined 3
Category: Required
Analysis: Decidable,Single Translation Unit
Rationale
The offsetof macro is used to access the underlying representation of an object, breaking its encapsulation. In addition, its use
results in undefined behaviour when the specified member is a bit-field, a static data member, or a member function.
Example
struct A
{
int32_t i;
};
void f1()
{
offsetof( A, i ); // Non-compliant
}
Copyright The MISRA Consortium Limited © 2023