Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Expand Minimize
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.

Compiler Warning (level 3) C4267

Error Message

'var' : conversion from 'size_t' to 'type', possible loss of data

When compiling with /Wp64, or when compiling on a 64-bit operating system, type is 32 bits but size_t is 64 bits when compiling for 64-bit targets.

To fix this warning, use size_t instead of a type.

Example

The following sample generates C4267.

// C4267.cpp
// compile with: /W3 /Wp64
#include <Windows.h>
void Func(int i) {}
void Func2(DWORD i) {}
void Func3(size_t i) {}

int main() {
   size_t bufferSize = 10;
   Func(bufferSize);   // C4267
   Func2(bufferSize);   // C4267
   Func3(bufferSize);   // OK
}

C4267 can also be caused on x86 and this warning cannot be resolved in code, but can be ignored and suppressed with the warning pragma.

In this situation, C4267 is caused by a limitation in /Wp64 warnings. On x86, std::cout<<range_index resolves to the overload of operator<< that accepts an unsigned int, since size_t is an unsigned int on Win32. C4267 occurs because you passed a size_t as an unsigned int argument, which would cause truncation on Win64 where size_t is 64-bit, but unsigned int is still 32-bit. This can be ignored because if you compiled for Win64, std::cout<<range_index would resolve to the overload of operator<< that accepts an unsigned __int64, since that's what type size_t is on Win64. The 32-bit compiler doesn't notice, so it warns.

The following sample generates C4267.

// C4267_b.cpp
// compile with: /W3 /Wp64 /EHsc
// processor: x86
#include <fstream>
#include <vector>
using namespace std;

int main() {
   ofstream outputfile;
   vector<char> vecbuffer(10);
   outputfile.write( &vecbuffer[0], vecbuffer.size() );   // C4267
}

Community Additions

ADD
Show:
© 2015 Microsoft