Dima Dima - 2 months ago 42
C Question

linux kernel `min` macros

I was looking through linux kernel source code (

) and I found this macro for

#ifndef max
#define max(x, y) ({ \
typeof(x) _max1 = (x); \
typeof(y) _max2 = (y); \
(void) (&_max1 == &_max2); \
_max1 > _max2 ? _max1 : _max2; })

And now I'm wondering what does
(void) (&_max1 == &_max2);
line do?

Kay Kay

It prevents accidental type casting of x or y. You can arithmetical compare differently sized integers with the same sign, but you must not compare their pointers. I.e. this code would generate a compiler warning:

short a = 47;
long b = 11;
min(a, b);

C.f. Is comparing two void pointers to different objects defined in C++?