...
Code Block |
---|
|
void func(void) {
int a = 14;
int b = sizeof(a);
++a;
} |
Noncompliant Code Example (sizeof
,
...
VLA)
In this noncompliant code example, the expression ++n
in the initialization expression of a
must be evaluated because its value affects the size of the VLA operand of the sizeof
operator. However, because the expression ++n % 1
evaluates to 0
, regardless of the value of n
, its value does not affect the result of the sizeof
operator. Consequently, it is unspecified whether or not n
is incremented.
Code Block |
---|
|
#include <stddef.h>
void f(size_t n) {
/* n must be incremented */
size_t a = sizeof(int[++n]);
/* n need not be incremented */
size_t b = sizeof(int[++n % 1 + 1]);
/* ... */
}
|
Compliant Solution (sizeof
,
...
VLA)
This compliant solution avoids changing the value of the variable n
used in the sizeof
expression and instead increments it safely outside of it:
...