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

B

baumann@pan

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
 
B

Ben Pfaff

baumann@pan said:
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.
 
K

Keith Thompson

baumann@pan said:
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>
 
C

Christian Bau

"baumann@pan said:
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top