Search code examples
clintmisra

MISRA rule 13.6 explanation needed


Rule 13.6 states that : The operand of the sizeof() operator should not contain any
expression which has potential side affects.I am at a loss to determine how an operator
with side effects will affect the outcome of sizeof() operator.
Can anybody explain with this example ?

void f(int32_t n)
{
    size_t s;
    s = sizeof(int32_t[n]); //Compliant
    s = sizeof(int32_t[n++]); //Non Compliant
}

Or can anybody give an example of a more understandable example and explain with that ?


Solution

  • The expression that is input to the sizeof operator is not evaluated. Only the type of the expression is deduced and used as input to the sizeof operator. Which means that, in the case that is non compliant to MISRA, n++, n will not be incremented. As this is somewhat counter-intuitive, MISRA recommends not to use expressions with side effects to avoid errors.