469,898 Members | 1,792 Online

# what's the help of "unnecessary" pointer comparison?

hi all,

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.

anyone could explain it for me? TIA.
baumann@pan

Nov 14 '05 #1
3 2191
"baumann@pan" <ba*********@gmail.com> writes:
i could not understand the "unnecessary" pointer comparison.

211 #define min(x,y) ({ \
212 typeof(x) _x = (x); \
213 typeof(y) _y = (y); \
214 (void) (&_x == &_y); \
215 _x < _y ? _x : _y; })

This uses GCC extensions. However, the intent of line 214 is to
ensure that _x and _y have compatible type by comparing their
addresses; only if _x and _y have compatible types will pointers
to them have compatible type. The result of the comparison is
ignored, so the only effect is to provoke a diagnostic from the
compiler if _x and _y have incompatible types.

The goal is to avoid taking the minimum of values of different
types, because the result can be surprising. For example, given
declarations
int x = -1;
unsigned y = 1;
the value of x > y ? x : y is UINT_MAX. This sort of surprising
result does not occur if x and y are both ints.
--
Ben Pfaff
email: bl*@cs.stanford.edu
web: http://benpfaff.org
Nov 14 '05 #2
"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.
Nov 14 '05 #3
In article <11**********************@f14g2000cwb.googlegroups .com>,
"baumann@pan" <ba*********@gmail.com> wrote:
hi all,

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.

anyone could explain it for me? TIA.

Apart from the fact that this is not C, but probably a language with a
certain similarity to C...

Try min (2, 4.7f). Should it work? Does it work?
Nov 14 '05 #4

### This discussion thread is closed

Replies have been disabled for this discussion.