C 中带括号和不带括号的循环处理方式不同吗?

发布于 2024-08-12 07:07:18 字数 478 浏览 4 评论 0原文

我在调试器中单步执行一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

禾厶谷欠 2024-08-19 07:07:18

调试器一次执行一个语句

检查一下:

int sum = 0;                            /* one assignment statement */
for (int k = 0; k < 10; k++) sum += k;  /* one for statement */

并与此进行比较

int sum = 0;                            /* one assignment statement */
for (int k = 0; k < 10; k++)
{                                       /* for statement with the body
                                           in a block of statements */
    sum += k;                           /* assignment statement */
}

在上面的第一个示例中,sum += kfor 语句的组成部分;在第二个示例中,它本身就是一个完整的语句。

The debugger executes one statement at a time.

Check this out:

int sum = 0;                            /* one assignment statement */
for (int k = 0; k < 10; k++) sum += k;  /* one for statement */

and compare with this

int sum = 0;                            /* one assignment statement */
for (int k = 0; k < 10; k++)
{                                       /* for statement with the body
                                           in a block of statements */
    sum += k;                           /* assignment statement */
}

In the first example above, the sum += k is an integral part of the for statement; in the 2nd example, it is a full statement on its own.

開玄 2024-08-19 07:07:18

“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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文