由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - what's wrong with this for() loop?
相关主题
new了指针,delete的时候出错了来来来,我也问个题 (转载)
问一个在C里面转换十六进制的问题Embedded C 编程问题求助
[转载] Re: 问个土问题吧有没有什么简单的方法从一个double precision的floating point 中读出一个特定的bit?
Another question问个overloading new operator的问题
little endian vs big endian内存泄露了吗?
请教一个C的问题c的小问题
template metaprogramming 的问题[合集] 问个面试题
一道面试题一个hash table的简单问题
相关话题的讨论汇总
话题: delta话题: loop话题: unsigned话题: wrong话题: int
进入Programming版参与讨论
1 (共1页)
i*****s
发帖数: 438
1
I was reading CSAPP2 and couldn't get this example
(regarding singed/unsigned integer):
#define DELTA sizeof(int)
int i;
for (i = CNT; i-DELTA >= 0; i-= DELTA)
. . .
the for() loop will not stop?
Thanks.
t****t
发帖数: 6806
2
sizeof() has unsigned type. as a result of [usual arithmetic conversions] (
standard 5, clause 9), in "i-DELTA", int is converted to unsigned; then "i-
DELTA>=0", 0 is converted to unsigned. thus the condition is always true,
since by definition, any unsigned number is >=0.

【在 i*****s 的大作中提到】
: I was reading CSAPP2 and couldn't get this example
: (regarding singed/unsigned integer):
: #define DELTA sizeof(int)
: int i;
: for (i = CNT; i-DELTA >= 0; i-= DELTA)
: . . .
: the for() loop will not stop?
: Thanks.

i*****s
发帖数: 438
3

谢谢!以后还请多赐教!

【在 t****t 的大作中提到】
: sizeof() has unsigned type. as a result of [usual arithmetic conversions] (
: standard 5, clause 9), in "i-DELTA", int is converted to unsigned; then "i-
: DELTA>=0", 0 is converted to unsigned. thus the condition is always true,
: since by definition, any unsigned number is >=0.

1 (共1页)
进入Programming版参与讨论
相关主题
一个hash table的简单问题little endian vs big endian
急问:这个为什么不行?请教一个C的问题
问个很基础的问题template metaprogramming 的问题
c++ template中如何判断类型一道面试题
new了指针,delete的时候出错了来来来,我也问个题 (转载)
问一个在C里面转换十六进制的问题Embedded C 编程问题求助
[转载] Re: 问个土问题吧有没有什么简单的方法从一个double precision的floating point 中读出一个特定的bit?
Another question问个overloading new operator的问题
相关话题的讨论汇总
话题: delta话题: loop话题: unsigned话题: wrong话题: int