Side effect of expression ignored
sizeof
, _Alignof
, or
_Generic
operates on expression with side effect
Description
This defect occurs when the
sizeof
, _Alignof
, or
_Generic
operator operates on an expression with a side effect.
When evaluated, an expression with side effect modifies at least one of the variables in
the expression.
For instance, the defect checker does not flag sizeof(n+1)
because
n+1
does not modify n
. The checker flags
sizeof(n++)
because n++
is intended to modify
n
.
The check also applies to the C++ operator alignof
and its C
extensions, __alignof__
and __typeof__
.
Risk
The expression in a _Alignof
or _Generic
operator is not evaluated. The expression in a sizeof
operator is
evaluated only if it is required for calculating the size of a variable-length
array, for instance, sizeof(a[n++])
.
When an expression with a side effect is not evaluated, the variable modification from the side effect does not happen. If you rely on the modification, you can see unexpected results.
Fix
Evaluate the expression with a side effect in a separate statement, and then use
the result in a sizeof
, _Alignof
, or
_Generic
operator.
For instance, instead of:
a = sizeof(n++);
n++; a = sizeof(n);
The checker considers a function call as an expression with a side effect. Even if
the function does not have side effects now, it might have side effects on later
additions. The code is more maintainable if you call the function outside the
sizeof
operator.
Examples
Result Information
Group: Programming |
Language: C | C++ |
Default: On for handwritten code, off for generated code |
Command-Line Syntax:
SIDE_EFFECT_IGNORED |
Impact: Low |
Version History
Introduced in R2018a
See Also
MISRA C:2012 Rule
13.6
| Redundant expression in sizeof
operand
| Find
defects (-checkers)
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)