Main Content

MISRA C:2023 Rule 13.6

The operand of the sizeof operator shall not contain any expression which has potential side effects

Since R2024a

Description

Rule Definition

The operand of the sizeof operator shall not contain any expression which has potential side effects.

Rationale

The argument of a sizeof operator is usually not evaluated at run time. If the argument is an expression, you might wrongly expect that the expression is evaluated.

Polyspace Implementation

The rule checker reports a violation if the operand of a sizeof operator has side effects that persist beyond the sizeof expression. For instance, if the sizeof operator increments a global variable, the rule checker reports a violation.

Troubleshooting

If you expect a rule violation but do not see it, refer to Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

#include <stddef.h>
int x;
int y[40];
struct S {
    int a;
    int b;
};
struct S myStruct;

void main() {
    size_t sizeOfType;
    sizeOfType = sizeof(x);         /* Compliant */
    sizeOfType = sizeof(y);         /* Compliant */
    sizeOfType = sizeof(myStruct);  /* Compliant */
    sizeOfType = sizeof(x++);       /* Non-compliant */
}

In this example, the rule is violated when the expression x++ is used as argument of sizeof operator.

Check Information

Group: Side Effects
Category: Required
AGC Category: Required

Version History

Introduced in R2024a