Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cant understand the implementation of max function in the linux kernel

Tags:

c

linux

What's the significance of (void) (&_max1 == &_max2); in the following definition of max found in Linux/tools/lib/lockdep/uinclude/linux/kernel.h?

#define max(x, y) ({                            \
      typeof(x) _max1 = (x);                  \
      typeof(y) _max2 = (y);                  \
      (void) (&_max1 == &_max2);              \
      _max1 > _max2 ? _max1 : _max2; })
like image 757
user3740387 Avatar asked Jan 23 '26 18:01

user3740387


1 Answers

It helps the compiler detect invalid uses of max(), i.e. with non-comparable x and y. As Sukminder points out, the == check is only used at compile time, it doesn't end up in the resulting binary.

like image 122
Stephen Kitt Avatar answered Jan 26 '26 11:01

Stephen Kitt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!