Warning C6334
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
}
See also
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for