Conversion from size_t to DWORD, possible loss of data

1.9k views Asked by At

I'm building a 64bit C++ code on VS 2015.

DWORD testVar;
testVar= strLen((LPCSTR)src);
// where src is a CString.

Seeing Warning - C4267 'argument': conversion from 'size_t' to 'DWORD', possible loss of data.

Any suggestions will be helpful.

1

There are 1 answers

6
Daniel H On BEST ANSWER

The error message says that it’s converting from size_t. This means that the original value has type size_t. Unless you have a reason you need to have a DWORD instead, you should keep the same type, so you should instead do

size_t testVar = strLen((LPCSTR)src);

You should keep the same data type because there is no chance of losing information that way, and it helps keep your application future-proof. If you used a 64-bit integer (which size_t probably is, because you’re on a 64-bit system), then you’d waste space if you ever wanted to compile for a 32-bit system, and you wouldn’t have enough space if you had more than 64 bits in a size_t (which is probably pretty far off, but there are some specialized areas now where it would be useful even though it isn’t yet practical so who knows). In general, you should not convert to a different type until you need to, and for this you don’t need to yet.