469,898 Members | 1,792 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,898 developers. It's quick & easy.

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.

Similar topics

188 posts views Thread by infobahn | last post: by
16 posts views Thread by jose_luis_fdez_diaz_news | last post: by
23 posts views Thread by bjk of course | last post: by
1 post views Thread by The Late Nate the Great | last post: by
49 posts views Thread by matty | last post: by
5 posts views Thread by Kelth.Raptor | last post: by
29 posts views Thread by Java script Dude | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.