[英]Conversion from size_t to DWORD, possible loss of data
I'm building a 64bit C++ code on VS 2015. 我正在VS 2015上构建64位C ++代码。
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. 看到警告-C4267'argument':从'size_t'到'DWORD'的转换,可能丢失数据。
Any suggestions will be helpful. 任何建议都会有所帮助。
The error message says that it's converting from size_t
. 错误消息表明它正在从 size_t
转换。 This means that the original value has type size_t
. 这意味着原始值的类型为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 除非您有理由需要DWORD
,否则应保持相同的类型,因此应改为
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). 如果您使用64位整数(因为在64位系统上,则可能是size_t
),那么如果您想为32位系统进行编译,则会浪费空间,而您不会如果size_t
中有64位以上,则有足够的空间(这可能相差很远,但是现在有一些专门的区域,即使它尚不实用,它还是很有用的,谁知道)。 In general, you should not convert to a different type until you need to, and for this you don't need to yet. 通常,除非需要,否则不应转换为其他类型,为此,您不需要这样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.