Warning related to size_t
I am trying to port an application from 32-bit to 64-bit windows environment. While compiling i get a lot of warnings related to"size_t".
This is because the size of the variable"size_t" changes from 32-bit to 64-bit on a 64-bit platform. The kind of warnings which i am getting are:-
sumAgg.cxx(370) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data
sumAgg.cxx(1105) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data
sumAgg.cxx(1109) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data
sumAgg.cxx(1113) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data
These warnings are at many places.
Please suggest ways to eliminate this warning.
Thanks,
- Navin
size_t and int are not necessarily interchangeable. You should avoid casting from a fixed-size type to a type that changes per-architecture and vice versa if at all possible for this exact reason.
Without a little more context as to what you are trying to build here, I can't really give the best solution.
-Alex
I am having similar problems with a similar error message being displayed. When I compile the following I get a C4267 warning.
main()
{
size_t tVariable = 34;
std::cout << tVariable << std::endl; //C4267 warning
}
I've used the line: std::cout << (unsigned int)tVariable << std::endl instead but this is obviously problematic for when tVariable is greater than UINT_MAX.
Joe
Here's what msdn says:
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.
....
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:
#pragma warning( disable : 4267 )
You should, however, use 'unsigned int' instead of 'int', when working with 'size_t' type .