C 中带括号和不带括号的循环处理方式不同吗?
我在调试器中单步执行一些 C/CUDA 代码,类似于:
for(uint i = threadIdx.x; i < 8379; i+=256)
sum += d_PartialHistograms[blockIdx.x + i * HISTOGRAM64_BIN_COUNT];
我完全困惑了,因为调试器一步就经过了它,尽管输出是正确的。我意识到,当我将大括号放在循环周围(如下面的代码片段所示)时,它在调试器中的行为符合预期。
for(uint i = threadIdx.x; i < 8379; i+=256) {
sum += d_PartialHistograms[blockIdx.x + i * HISTOGRAM64_BIN_COUNT];
}
因此,在 C 或调试器中,无括号 for 循环的处理方式不同,或者可能是 CUDA 特有的。
谢谢
I was stepping through some C/CUDA code in the debugger, something like:
for(uint i = threadIdx.x; i < 8379; i+=256)
sum += d_PartialHistograms[blockIdx.x + i * HISTOGRAM64_BIN_COUNT];
And I was utterly confused because the debugger was passing by it in one step, although the output was correct. I realised that when I put curly brackets around my loop as in the following snippet, it behaved in the debugger as expected.
for(uint i = threadIdx.x; i < 8379; i+=256) {
sum += d_PartialHistograms[blockIdx.x + i * HISTOGRAM64_BIN_COUNT];
}
So is are parenthesis-free for loops treated differently in C or in the debugger, or perhaps it is particular to CUDA.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
调试器一次执行一个语句。
检查一下:
并与此进行比较
在上面的第一个示例中,
sum += k
是for
语句的组成部分;在第二个示例中,它本身就是一个完整的语句。The debugger executes one statement at a time.
Check this out:
and compare with this
In the first example above, the
sum += k
is an integral part of thefor
statement; in the 2nd example, it is a full statement on its own.“for”后面的单个语句与其中包含一个语句的块之间没有任何执行差异。不过,看看你的代码,你是否意识到 i 实际上并没有增加?也许你的意思是输入 i+=256。
就调试器而言,括号构成了要“移入”的其他内容,而单行只是单行(就像没有块的 if 语句一样)。
There isn't any execution difference between a single statement following the "for" or a block with one statement in it. Looking at your code though, do you realise that i isn't actually incremented? Perhaps you meant to put i+=256.
As far as the debugger is concerned the brackets constitute something else to "move into" whereas the single line is just that, a single line (just like an if statement with no block).