473,769 Members | 6,337 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

what's the help of "unnecessar y" pointer comparison?


hi all,

i could not understand the "unnecessar y" pointer comparison.

/*
207 * min()/max() macros that also do
208 * strict type-checking.. See the
209 * "unnecessar y" 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 "unnecessar y" pointer
comparison.

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

Nov 14 '05 #1
3 2485
"baumann@pa n" <ba*********@gm ail.com> writes:
i could not understand the "unnecessar y" 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@pa n" <ba*********@gm ail.com> writes:
i could not understand the "unnecessar y" pointer comparison.

/*
207 * min()/max() macros that also do
208 * strict type-checking.. See the
209 * "unnecessar y" 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 "unnecessar y" 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_Keit h) 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************ **********@f14g 2000cwb.googleg roups.com>,
"baumann@pa n" <ba*********@gm ail.com> wrote:
hi all,

i could not understand the "unnecessar y" pointer comparison.

/*
207 * min()/max() macros that also do
208 * strict type-checking.. See the
209 * "unnecessar y" 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 "unnecessar y" 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
17433
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
16
5861
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 compiles fine. What is the warning shown ?
4
2815
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
2176
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
4653
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 double-clicking on the error did nothing. Knowing how many problems there have been in Microsnot's tools throughout this project, I tried simply closing VisStudio and reopening the project. The error mysteriously vanished.
49
14524
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 http://www.webreference.com/programming/javascript/gr/column9/ they say: <snip> The undefined property A relatively recent addition to JavaScript is the undefined property.
59
7521
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 the recordset opens a table. When I write Set rst = db.OpenRecordset("MyTable",dbOpenTable, dbReadOnly) I get an error. I believe it's invalid operation or invalid parameter, I'm
5
1846
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 thought I would go a bit above and beyond the scope of the class and use strings. But I ran into a snag with my getgrades function. The compiler gives me the error: "81 ISO C++ forbids comparison between pointer and integer" here is the code for the...
29
2434
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 back to the original spec for JavaScript. Not very intuitive but I guess I can code around this.
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10050
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9999
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9866
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8876
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3570
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.