Compilerwarnung (Ebene 3) C4267

'var': Konvertierung von 'size_t' nach 'type', Datenverlust möglich

Bemerkungen

Der Compiler hat eine Konvertierung von size_t in einen kleineren Typ erkannt, wodurch der Wert möglicherweise abgeschnitten wird.

Verwenden Sie zum Beheben dieser Warnung size_t anstelle von type. Alternativ können Sie einen integralen Typ verwenden, der den gesamten Bereich size_t auf der Zielplattform darstellen kann. Wenn der Wert zur Kompilierungszeit bekannt ist und in den Zieltyp passt, generiert der Compiler diese Warnung nicht.

Beispiel

Im folgenden Beispiel wird der Fehler C4267 generiert:

// C4267.cpp
// compile by using: cl /W4 C4267.cpp
void Func1(short) {}
void Func2(int) {}
void Func3(long) {}
void Func4(size_t) {}

int main() {
   size_t bufferSize = 10;
   Func1(bufferSize);   // C4267 for all platforms
   Func2(bufferSize);   // C4267 only for 64-bit platforms
   Func3(bufferSize);   // C4267 only for 64-bit platforms
   Func4(bufferSize);   // OK for all platforms

   // Because the compiler knows the value of bufferSize_const at compile time (because it's const),
   // it knows that converting it to short can't truncate the value so no warning is emitted
   const size_t bufferSize_const = 10;
   Func1(bufferSize_const); // No C4267

   // Because the compiler knows the value of bufferSize_big_const is 0x10000 at compile time
   // (because its const), it knows it doesn't fit in short, but does fit in int.
   const size_t bufferSize_big_const = 0x10000;
   Func1(bufferSize_big_const); // C4267
   Func2(bufferSize_big_const); // No C4267

   return 0;
}