"baumann@pan" <ba*********@gmail.com> writes:
i could not understand the "unnecessary" pointer comparison.
/*
207 * min()/max() macros that also do
208 * strict type-checking.. See the
209 * "unnecessary" pointer comparison.
210 */
211 #define min(x,y) ({ \
212 typeof(x) _x = (x); \
213 typeof(y) _y = (y); \
214 (void) (&_x == &_y); \
215 _x < _y ? _x : _y; })
216
217 #define max(x,y) ({ \
218 typeof(x) _x = (x); \
219 typeof(y) _y = (y); \
220 (void) (&_x == &_y); \
221 _x > _y ? _x : _y; })
222
what's the meaing of line 214 and line 220?
IMHO, the result of the 2 is always FALSE. since &_x is the address of
loccal variable _x and &_y is the address of local variable _y.
so I can not figure out the meaning of "unnecessary" pointer
comparison.
But both macros are extremely non-portable. The typeof() operator and
statement expressions are both gcc extensions, not part of standard C.
<OT>
Assuming (as the name "typeof" implies) that _x has the same type as
x, and _y has the same type as y, comparing the addresses of _x and _y
is legal if and only if they have the same type. The result of the
comparison is irrelevant (and it's discarded anyway); the point is to
force the compiler to reject an invocation of the macro if the
arguments' types don't match.
</OT>
--
Keith Thompson (The_Other_Keith)
ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.