Dima Dima - 1 month ago 22
C Question

linux kernel `min` macros

I was looking through linux kernel source code (

kernel.h
) and I found this macro for
min
function:

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


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

Kay Kay
Answer

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++?