3

In the linux kernel (ASOP) code I came across the following macro code:

#define for_each_cpu_and(cpu, mask, and)    \
    for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask, (void)and)

I have never come across a for loop like the one above where there are several comma separated variables along with the increment part. Using the following code I tried to check how the aforementioned macro actually behaves:

#include <stdio.h>
#include <stdbool.h>

#define for_each_cpu_and(cpu, mask, and)    \
    for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask, (void)and)

int main()
{
    int i;
    for_each_cpu_and(i, false, 3){
      printf("i value: %d\n", i);
    }
    return 0;
}

The answer in the console is as follows:

i value: 0

If I tweek the code to the following:

#include <stdio.h>
#include <stdbool.h>

#define for_each_cpu_and(cpu, mask, and)    \
    for ((cpu) = 0; (cpu) < 3; (cpu)++, (void)mask, (cpu)+2)

int main()
{
    int i;
    for_each_cpu_and(i, 4, 3){
      printf("i value: %d\n", i);
    }
    return 0;
}

The answer in the console is as follows:

i value: 0

i value: 1

i value: 2

So from the aformentioned code it seems like in the increment part only the first increment option i.e. (cpu)++ is given precedence and others are not being used.

Can someone please explain with example(s) the usage of additional comma separated variables in the increment part of the for loop?

Note: I am aware what a comma separated variable in C does in general and based on the rule the first varaible in the code should be given precedence. However, in the aforementioned code the case is not true. So explanation on the working of the comma separated variables in the increment part of the for-loop in the aforementioned code would be much appreciated.

Comma separated variables in C: How does the Comma Operator work

Community
  • 1
  • 1
Somdip Dey
  • 3,346
  • 6
  • 28
  • 60
  • I actually never thought about that apart from incrementing several variables at once... Nice question! – M.K Apr 01 '19 at 10:01
  • @M.K thank you for your comment. Could you please vote to reopen the question? I think unwind (user) has closed the question based on the use of comma separated variables in C but the issue with the aformentioned question is that it is not following the rule OR I might be missing something. Thank you once again. – Somdip Dey Apr 01 '19 at 10:09
  • @unwind could you please vote to reopen the question since the general rule for comma separated variable in C is not working for the for loop based question I have asked. thank you. – Somdip Dey Apr 01 '19 at 10:10
  • The use of the comma is different to the one flagged. Unluckily I cannot vote to open, but I can flag it in need of moderator explaining that it is not a duplicate! – M.K Apr 01 '19 at 10:13
  • @M.K thank you so much for this help. Much appreciated. – Somdip Dey Apr 01 '19 at 10:14
  • @SomdipDey - You are missing the fact the third expression in a for loop may be any valid C expression. Including but not limited to expressions that use the **comma operator**. Which is *not* to be confused with the comma in comma separated declarations. – StoryTeller - Unslander Monica Apr 01 '19 at 10:17
  • @SomdipDey I'm not convinced, and "comma separated variables" is not what this is about, it's just an expression. – unwind Apr 01 '19 at 10:23
  • @unwind I have editied my question to showcase that the use of comma separated variable in the foor-loop is not working as a general comma separated varaible in C. and have reframed the question accordingly. could you please have a look at the edited question? – Somdip Dey Apr 01 '19 at 10:27
  • @StoryTeller thank you for your reply. I have edited my question to rephrase what I actually mean. could you please have a look at the edited question? – Somdip Dey Apr 01 '19 at 10:28
  • 1
    What exactly is the point of your edit? `(cpu) + 2` won't change `(cpu)` any more than `int i = 2; int j = i + 2;` would change `i`. – StoryTeller - Unslander Monica Apr 01 '19 at 10:35
  • 3
    The version of the macro you have posted is only defined for kernels that support a single CPU (`NR_CPUS == 1`). A different version of the macro is defined for "SMP" kernels that support several CPUs (`NR_CPUS > 1`). Presumably, the `(void)mask` and `(void)and` are only there to prevent some compiler warnings. – Ian Abbott Apr 01 '19 at 13:13

0 Answers0