Avviso del compilatore (livello 3) C4267

'var': conversione da 'size_t' a 'type'. Possibile perdita di dati.

Osservazioni:

Il compilatore ha rilevato una conversione da size_t a un tipo più piccolo che potrebbe troncare il valore.

Per risolvere l'avviso, usare size_t invece di type. In alternativa, usare un tipo intero che sia in grado di rappresentare l'intera gamma di size_t sulla piattaforma di destinazione. Se il valore è noto in fase di compilazione e rientra nel tipo di destinazione, il compilatore non genera questo avviso.

Esempio

L'esempio seguente genera l'errore C4267.

// 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;
}