473,386 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 2436
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

188
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
16
by: jose_luis_fdez_diaz_news | last post by:
Hi, If I don't include <libgen.h> I get then warnig below in regcmp call: warning: improper pointer/integer combination: op "=" but if I include it the warning is not shown, but them program...
4
by: PCHOME | last post by:
Hi! I have questions about qsort( ). Is anyone be willing to help? I use the following struct: struct Struct_A{ double value; ... } *AA, **pAA;
23
by: bjk of course | last post by:
hello, I've stumbled upon this in some code: n = !!x; I've ran it and 'n' is always either 0 or 1. Is '!!' an operator (what's it called?) and is it standard/portable? --
1
by: The Late Nate the Great | last post by:
Using VC# in creating a .NET Compact Framework project, I received this message first thing Monday morning (talk about a bad omen for the week!). It gave a line number that didn't apply, and...
49
by: matty | last post by:
Hi, I recently got very confused (well that's my life) about the "undefined" value. I looked in the FAQ and didn't see anything about it. On...
59
by: Rico | last post by:
Hello, I have an application that I'm converting to Access 2003 and SQL Server 2005 Express. The application uses extensive use of DAO and the SEEK method on indexes. I'm having an issue when...
5
by: Kelth.Raptor | last post by:
Im having some difficulty with strings here, I hope someone is kind enough to help, I do appreciate it. Im working on a grade point average calculator for my intro to programming class and I...
29
by: Java script Dude | last post by:
Greetings, Now I can understand that 0 equal false, but should "" also equal false? Apparently the answer is yes. When I test both Firefox and IE, they both say ""==false . I assume this goes...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.