0

I see this code in the Linux kernel but I am not able to understand why (void) f; is being called here. Any idea?

    /* start 'ping' in the background to have some kfree_skb events */
    f = popen("ping -c5 localhost", "r");
    (void) f;

Thanks

pchaigno
  • 11,313
  • 2
  • 29
  • 54
Breno Leitão
  • 3,487
  • 2
  • 19
  • 23
  • Suppress warnings. Possible duplicate of [What does casting to \`void\` really do?](https://stackoverflow.com/q/34288844/608639) – jww Feb 06 '19 at 15:49
  • I'm *guessing* that the variable `f` isn't used anywhere else? Then that's to suppress possible warnings about unused variables. Personally I would rather cast the call to `popen` instead of having the unused variable. – Some programmer dude Feb 06 '19 at 15:49
  • Usually `(void)f` is called to silence compiler complaints (if you don't directly reference it). – Micrified Feb 06 '19 at 15:49

1 Answers1

0

This piece of code, as per the comment, is using popen to start a background process. This function returns a FILE * to one end of the pipe.

This code however does not use the value of f. Typically, a compiler will print a warning if a variable is unused. Using f in an expression by itself casted to void uses the value of f but explicitly discards that value, preventing the warning from being printed.

dbush
  • 205,898
  • 23
  • 218
  • 273