Rule Definition
A possible programming error in C++ is to apply the sizeof operator to expression and expect the expression to be evaluated. However, the expression is not evaluated as sizeof only acts on the type of the expression. To avoid this error, sizeof shall not be used on expressions that would contain side effects if they were used elsewhere, as the side effects will not occur.
Remediation
Ensure to separate a side effect of an expression from the sizeof method
Violation Code Sample
int32_t i;
int32_t j;
j = sizeof( i = 1234 ); // Non-compliant - j is set to the sizeof the
// type of i which is an int32_t.
// i is not set to 1234.
Fixed Code Sample
int32_t j;
volatile int32_t k;
j = sizeof ( k ); // Compliant by exception.
Reference
MISRA C++ 2008, 5-3-4: Evaluation of the operand to the sizeof operator shall not contain side effects.
Related Technologies
Technical Criterion
Programming Practices - Unexpected Behavior
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.