Udostępnij za pośrednictwem


C6260

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. The C/C++ sizeof operator returns the number of bytes of storage an object uses. It is typically incorrect to multiply it by another sizeof operation; usually one is 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 is some unintuitive behavior associated with sizeof operator. For example, in C, the 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 is true:

sizeof ("\0") == 2. 

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( )
{
  int i;
  i= sizeof (L"String") / sizeof (WCHAR);
  
  /* or to get bytes */
  i = sizeof (L"String");
  // code ...
}

See Also

Reference

sizeof Operator

sizeof Operator (C)