Why is this an issue?
This is a draft version of a MISRA C++ 202x rule proposed for public review.
MISRA Rule 18.2.4
Category: Required
Analysis Type: 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
Resources
Related rules
S986 targets the same defect as this rule but for a non-mission-critical context.