Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
sizeof
operator applied to an expression with an operator may yield unexpected results
Remarks
The sizeof
operator, when applied to an expression, yields the size of the type of the resulting expression.
Code analysis name: SIZEOFEXPR
Example
The following code generates this warning. Since a - 4
is an expression, sizeof
will return the size of the resulting pointer, not the size of the structure found at that pointer:
void f( )
{
size_t x;
char a[100];
x = sizeof(a - 4);
assert(x == 96); //assert fails since x == sizeof(char*)
}
To correct this warning, ensure that you're working with the return value of sizeof
, not the argument to it:
void f( )
{
size_t x;
char a[100];
x = sizeof(a) - 4;
assert(x == 96); //assert succeeds
}