为什么从 size_t 转换为 unsigned int 会给我一个警告?
我有代码:
unsigned int length = strlen(somestring);
我正在使用 4 的警告级别进行编译,它告诉我“从 size_t
转换为 unsigned int
,可能会丢失数据”,当size_t
是 unsigned int
的 typedef
。
为什么!?
编辑:
我刚刚解决了我自己的问题。我是 XP 用户,我的编译器正在检查 64 位兼容性。由于 size_t
与平台相关,因此对于 64 位,它将是一个 unsigned long long
,与 unsigned int
不同。
I have the code:
unsigned int length = strlen(somestring);
I'm compiling with the warning level on 4, and it's telling me that "conversion from size_t
to unsigned int
, possible loss of data" when a size_t
is a typedef
for an unsigned int
.
Why!?
Edit:
I just solved my own problem. I'm an XP user, and my compiler was checking for 64 bit compatibility. Since size_t
is platform dependent, for 64 bit it would be an unsigned long long
, where that is not the same as an unsigned int
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为
unsigned int
在您的机器上是比size_t
更窄的类型。size_t
很可能是 64 位宽,而unsigned int
是 32 位宽。编辑:size_t 不是 unsigned int 的 typedef。
Because
unsigned int
is a narrower type on your machine thansize_t
. Most likelysize_t
is 64 bits wide, whileunsigned int
is 32 bits wide.EDIT: size_t is not a typedef for unsigned int.