无符号整型算术 - C 初学者
冒着因发布一个愚蠢问题而被骂的风险:
假设我有以下代码片段:
unsigned int limit = 4294967296;
int math = 50000;
while(limit > 50000)
{
limit = limit - math;
printf("new limit is - \t%u\n", limit);
}
在 unsigned int 类型和 int 类型之间进行算术会对内存使用产生影响吗?
我知道 unsigned int 的内存要多一个数量级(int 为 2^31,unsigned int 为 2^32),但是,由于我使用 unsigned int 作为占位符,所以我认为我正在否定溢出的危险。
我会自己运行这个,但我现在没有可访问的 Linux 盒子。
对此的任何意见将不胜感激。
At the risk of getting yelled at for posting a stupid question:
say I have the following code snippet:
unsigned int limit = 4294967296;
int math = 50000;
while(limit > 50000)
{
limit = limit - math;
printf("new limit is - \t%u\n", limit);
}
Would doing arithmetic between an unsigned int type and an int type have rammifications o n memory usage?
I know that an unsigned int has an order of magnitude more memory (2^31 for an int vs 2^32 for an unsigned int), but, since I'm using my unsigned int as a placeholder, i think I'm negating the danger of an overflow.
I would run this myself, but I don't have an accessible linux box right now.
Any input on this would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这将永远循环,因为您最终将得到环绕(即
limit
永远不会小于 0)...这是您的问题吗?This will loop forever since you will end up with wrap-around (i.e.,
limit
will never be less than 0) ... is that your question?