SIZEOF-side-effect
Synopsis
sizeof expressions containing side effects
Enabled by default
Yes
Severity/Certainty
Medium/Medium

Full description
The sizeof operator is used on an expression that contains side effects. Because sizeof only operates on the type of the expression, the expression itself is not evaluated, which it probably was meant to be. This check is identical to MISRAC2004-12.3, MISRAC++2008-5-3-4.
Coding standards
- CERT EXP06-C
Operands to the sizeof operator should not contain side effects
- CERT EXP06-CPP
Operands to the sizeof operator should not contain side effects
- MISRA C:2004 12.3
(Required) The sizeof operator shall not be used on expressions that contain side effects.
- MISRA C++ 2008 5-3-4
(Required) Evaluation of the operand to the sizeof operator shall not contain side effects.
Code examples
The following code example fails the check and will give a warning:
void example(void) {
int i;
int size = sizeof(i++);
}
The following code example passes the check and will not give a warning about this issue:
void example(void) {
int i;
int size = sizeof(i);
i++;
}