Are loops with and without parentheses handled differently in C?

I skipped the C / CUDA code in the debugger, for example:

for(uint i = threadIdx.x; i < 8379; i+=256) 
    sum += d_PartialHistograms[blockIdx.x + i * HISTOGRAM64_BIN_COUNT];

And I was completely embarrassed because the debugger passed it in one step, although the solution was right. I realized that when I put curly braces 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];
}

Thus, for loops handled differently in C or in the debugger, there are no parentheses, or perhaps this applies to CUDA.

thanks

+3
source share
2 answers

The debugger executes one statement at a time. Check this:

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 */
}

sum += k for; .

+10

, "for" . , , ? , + = 256.

, , "", - ( , if ).

+4

Source: https://habr.com/ru/post/1723653/


All Articles