Warning C6260
sizeof
*sizeof
is almost always wrong, did you intend to use a character count or a byte count?
This warning indicates that the results of two sizeof
operations have been multiplied together.
Remarks
The C/C++ sizeof
operator returns the number of bytes of storage an object uses. It's typically incorrect to multiply it by another sizeof
operation. Usually, you're interested in the number of bytes in an object or the number of elements in an array (for example, the number of wide-characters in an array).
There's some unintuitive behavior associated with sizeof
operator. For example, in C, sizeof ('\0') == 4
, because a character is of an integral type. In C++, the type of a character literal is char
, so sizeof ('\0') == 1
. However, in both C and C++, the following relation is true:
sizeof ("\0") == 2
Code analysis name: USEOFBYTEAREA
Example
The following code generates this warning:
#include <windows.h>
void f( )
{
int i;
i = sizeof (L"String") * sizeof (WCHAR);
// code ...
}
To correct this warning, use the following code:
#include <windows.h>
void f( )
{
// use divide to calculate how many WCHAR characters are in the string
int i = sizeof (L"String") / sizeof (WCHAR);
// get the number of bytes in the character array
int j = sizeof (L"String");
// code ...
}