473,703 Members | 3,018 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Comparing pointers

In C you can compare two pointers, p<q, as long as they come from the
same array or the same malloc()ated block. Otherwise you can't.

What I'd like to do is write a function
int comparable(void *p, void *q)
that will take any two pointers and decide whether they can be compared
or not.

I really can't think how to do this - any suggestions?

JC.

Jun 5 '07 #1
25 13031
On Tue, 05 Jun 2007 23:44:57 +0200, J Caesar wrote:
In C you can compare two pointers, p<q, as long as they come from the
same array or the same malloc()ated block. Otherwise you can't.
Really!? How have I managed to do so all these years?

Pointers are just addresses - usually 32-, these days more and
more, 64-bit unsigned integers. Which you can always compare at will.
Usually you'll want for them to be pointers to the same type of data, of
course.

>
What I'd like to do is write a function int comparable(void *p, void *q)
that will take any two pointers and decide whether they can be compared
or not.

I really can't think how to do this - any suggestions?

JC.
Jun 5 '07 #2

"J Caesar" <in*****@nospam .comwrote in message
news:sl******** ************@no spam.com...
In C you can compare two pointers, p<q, as long as they come from the
same array or the same malloc()ated block. Otherwise you can't.

What I'd like to do is write a function
int comparable(void *p, void *q)
that will take any two pointers and decide whether they can be compared
or not.

I really can't think how to do this - any suggestions?
It can't be done. Or rather, it can't be done portably. To do it
non-portably is probably either trivial or the compiler has no issues with
pointers to different blocks.

The "no comparison" rule is a kludge to allow for funny architectures. It
becomes a nuisance when you need to know whether a pointer comes from a
certain block or not. However you don't need that information very
frequently, and on balance it is probably better to make the compiler easier
to implement or code to run faster.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Jun 5 '07 #3

"Ivar Rosquist" <IR*******@irq. orgwrote in message
news:pa******** *************@i rq.org...
On Tue, 05 Jun 2007 23:44:57 +0200, J Caesar wrote:
>In C you can compare two pointers, p<q, as long as they come from the
same array or the same malloc()ated block. Otherwise you can't.

Really!? How have I managed to do so all these years?
It is undefined behaviour. Realistically a flat memory architecture is just
going to do a starightforward s subtraction of bits. However it could trap or
return funny results on some machines.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Jun 5 '07 #4
In article <sl************ ********@nospam .com>,
J Caesar <in*****@nospam .comwrote:
>In C you can compare two pointers, p<q, as long as they come from the
same array or the same malloc()ated block. Otherwise you can't.

What I'd like to do is write a function
int comparable(void *p, void *q)
that will take any two pointers and decide whether they can be compared
or not.

I really can't think how to do this - any suggestions?
You're right, you can't. But why do you want to? What are you trying
to achieve?

Note that you can compare pointers for equality even if they are
from different objects.

-- Richard

--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Jun 5 '07 #5
In article <pa************ *********@irq.o rg>,
Ivar Rosquist <IR*******@irq. orgwrote:
>On Tue, 05 Jun 2007 23:44:57 +0200, J Caesar wrote:
>In C you can compare two pointers, p<q, as long as they come from the
same array or the same malloc()ated block. Otherwise you can't.
> Really!? How have I managed to do so all these years?
Non-portably.
> Pointers are just addresses - usually 32-, these days more and
more, 64-bit unsigned integers. Which you can always compare at will.
Usually you'll want for them to be pointers to the same type of data, of
course.
Pointers are not necessarily just addresses: pointers may be opaque
with storage silently managed "under the hood", or pointers may
include type information, or pointers may include not have the
address information in numeric order (e.g., Cray put the character
offset bits in the upper byte). Or, as was common on VMS, pointers
might be the address of "descriptor s" -- a block of data that
describes the type and array and virtual memory address of the
actual storage. And when you are working with a Harvard architecture,
a pointer to a function could have exactly the same numeric value
as a pointer to an object: since C does not provide any operations
that translate between function pointers and object pointers, there
is no conflict in having a function pointer with the same value
as an object pointer, with the code knowing which kind of instructions
are needed for the correct kind of access.
--
All is vanity. -- Ecclesiastes
Jun 5 '07 #6
On 5 Jun 2007 at 22:12, Richard Tobin wrote:
You're right, you can't. But why do you want to? What are you trying
to achieve?

Note that you can compare pointers for equality even if they are
from different objects.
This gives one method: write a wrapper to malloc to store a pointer to
each block allocated together with the size of the block. Then do an
exhaustive search over all pointers to all elements of all blocks
allocated to find which blocks p and q are in (only needs you to compare
each pointer for equality with p or q, which is legal). And you're done.

I see two problems: 1) very inefficient if large tracts of memory are
allocated; 2) fails if p or q point inside statically allocated arrays
rather than malloc()ed blocks.

JC.
-- Richard
Jun 5 '07 #7
On Jun 5, 11:02 pm, Ivar Rosquist <IRosqu...@irq. orgwrote:
On Tue, 05 Jun 2007 23:44:57 +0200, J Caesar wrote:
In C you can compare two pointers, p<q, as long as they come from the
same array or the same malloc()ated block. Otherwise you can't.

Really!? How have I managed to do so all these years?
By ignoring the C Standard and by avoiding any C implementations with
segmented memory.

Jun 5 '07 #8
In article <11************ **********@p47g 2000hsd.googleg roups.com>,
christian.bau <ch***********@ cbau.wanadoo.co .ukwrote:
In C you can compare two pointers, p<q, as long as they come from the
same array or the same malloc()ated block. Otherwise you can't.
> Really!? How have I managed to do so all these years?
>By ignoring the C Standard and by avoiding any C implementations with
segmented memory.
Which shows poor judgment but good taste.

-- Richard

--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Jun 5 '07 #9
J Caesar wrote, On 05/06/07 22:44:
In C you can compare two pointers, p<q, as long as they come from the
same array or the same malloc()ated block. Otherwise you can't.

What I'd like to do is write a function
int comparable(void *p, void *q)
that will take any two pointers and decide whether they can be compared
or not.

I really can't think how to do this - any suggestions?
Your initial thought is correct, you can't do the check in standard C.
--
Flash Gordon
Jun 5 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2669
by: Sayan | last post by:
How do I distinguish between a heap pointer and a stack pointer? I want the following to work: template<class T> bool isHeapPtr(T* p1,T* p2);//the function I want to write //... int a = 5; int *pas = &a; int *pah = new int; *pah = 10;
27
3124
by: glen herrmannsfeldt | last post by:
The subject recently came up in comp.compilers, though I believe that I asked here before. If you use relational operators, other than == and !=, on pointers to different objects, is there any requirement on the result? #include <stdio.h> int main() {
20
2148
by: Bill Pursell | last post by:
This question involves code relying on mmap, and thus is not maximally portable. Undoubtedly, many will complain that my question is not topical... I have two pointers, the first of which is mmapped to a single page. I want to determine if the second is on the page. I'd like to do: #include "platform_appropriate_definition_of_PAGESIZE.h" int compare1(const char *a, const char *b)
66
2679
by: Praveen | last post by:
Hi, I came across a program as follows main() { int x, y ; int *p1 = &x; int *p2 = &y; printf("%d\n", p1-p2); }
12
2561
by: Michael Bell | last post by:
I am trying to put my learning effort into the most useful things. What do pointers do that other things can't? Michael Bell --
0
8759
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
8669
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8963
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
7872
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...
1
6588
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5922
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4433
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
4687
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3125
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.