简体   繁体   中英

Conversion from size_t to DWORD, possible loss of data

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM