0

When I was looking for some inspiration, I stumbled across the following code:

(void)memset(&ifm, 0, sizeof(ifm));

https://github.com/i3/i3status/blob/master/src/print_eth_info.c#L55

I know (void) cast is used to suppress unused warnings in C. But I don't see any reason why a warning should be generated for not checking the return value of memset().

So may question is what is the reason for using void cast here.

Schafwolle
  • 501
  • 3
  • 15
  • There's no need to do it, and most programmers don't. – Barmar Apr 14 '20 at 21:12
  • 1
    It might be useful if you enable warnings about not using the results of functions. – Barmar Apr 14 '20 at 21:13
  • 3
    When you compile with `-Wall`, some/most warnings are enabled. Normally, `(void)` is not needed _unless_ you compile with `-Wunused-result` which _is_ enabled by default. But, you only get a warning if a given function is marked with the attribute "warn_unused_result" (per `man gcc`). So, normally, don't just add `(void)` unless you _have_ to, IMO – Craig Estey Apr 14 '20 at 21:17
  • @TonyTannous I guess this could be the reason. Thank you. Although its pretty obvious that the return of memset() can savely be ignored – Schafwolle Apr 14 '20 at 21:21
  • @CraigEstey I thought about the same, but I don't see any reason to mark memset as "warn_unuesed_result". – Schafwolle Apr 14 '20 at 21:23
  • 2
    @Schafwolle what might seem obvious to oneself, may not be for someone else. – Tony Tannous Apr 14 '20 at 21:23
  • I just blamed the file the (void) cast was introduced 11 years ago – Schafwolle Apr 14 '20 at 21:27
  • Basically, it's up to the discretion of the author of the _called_ function whether to use the attribute or not. Most functions _don't_ do this. So, using `(void)` is to _override_ those _rare_ ones that do. It says [effectively] "I know I don't use the return value and I _know_ it's okay". If this were a problem, I'd just add `-Wno-unused-result` to my makefile. Again, IMO, using `(void)` is just cruft, and makes the code _less_ readable. The compiler checks this now. IIRC, before you had to do: `int dummy; dummy = fnc();` or a special comment to get `lint` to shut up – Craig Estey Apr 14 '20 at 21:37
  • 1
    This might be useful if the an author changes a function signature from `void myfunc(void)` to `int myfunc(void)` [say 6 months later]. Not recommended practice if `myfunc` API has been published for an extended period of time. But, the [linux] kernel developers are willing to do such things (with justification) rather than leave bad code in the code base. – Craig Estey Apr 14 '20 at 21:41
  • `struct ifmediareq ifm; (void)memset(&ifm, 0, sizeof(ifm));` would have been cleaner as `struct ifmediareq ifm = { 0 };`. AFAIK, not only is the `(void)` unnecessary, the `memcpy()` is also avoidable. – chux - Reinstate Monica Apr 14 '20 at 21:56
  • VTC as dupe as " what is the reason for using void cast here." is "you ignore the return value, and did not just forget it." in the dupe. Certainly this is more valuable in functions like `scanf()` and family than in `mem..(a...), str...(a,...)` that always return `a`. – chux - Reinstate Monica Apr 14 '20 at 22:05

0 Answers0