简体   繁体   English

从 size_t 转换为 int 可能会丢失数据

[英]Possible Lost of Data Converting from size_t to int

Windows programming. Windows 编程。 Following the tutorial at this MS page and get to this line at the bottom:按照此MS 页面上的教程并到达底部的这一行:

TextOutW(hdc, 5, 5, greeting, wcslen(greeting));

Variable greeting is a TCHAR array.变量greeting是一个 TCHAR 数组。 VS2019 reports a warning (C4267) at the above line that there is a possible loss of data in the final parameter converting from the <size_t> returned by wcslen() and the int expected by TextOutW(). VS2019 在上一行报告了一个警告(C4267),从 wcslen() 返回的 <size_t> 和 TextOutW() 预期的 int 转换的最终参数中可能会丢失数据。 I know that the original example uses the _tcslen macro instead of wcslen() - it doesn't matter.我知道原始示例使用 _tcslen 宏而不是 wcslen() - 没关系。 I understand why that's happening but am completely mystified on how to modify the code to resolve the warning.我理解为什么会发生这种情况,但对如何修改代码以解决警告完全感到困惑。

I can't find another TextOutx() that might take a <size_t>.我找不到另一个可能需要 <size_t> 的 TextOutx()。

As an aside, while I was search for an answer, I read somewhere that strlen() is deprecated in 64-bit programming, although I can't recall where and I might have it wrong.顺便说一句,当我在寻找答案时,我在某处读到 strlen() 在 64 位编程中已被弃用,尽管我不记得在哪里,我可能错了。

Is there a solution?有解决办法吗? Any advice?有什么建议吗?

An explicit cast to (int)wcslen(greeting);显式转换为(int)wcslen(greeting); resolves the warnings but hides the potential loss of data.解决了警告,但隐藏了潜在的数据丢失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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