比较指针 [英] Comparing pointers

查看:64
本文介绍了比较指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C中你可以比较两个指针,p< q,只要它们来自

相同的数组或相同的malloc()ated块。否则你就不能。


我想做的就是写一个函数

int comparative(void * p,void * q)

将需要任意两个指针并决定是否可以比较




我真的不能想想怎么做 - 有什么建议吗?


JC。

解决方案

On Tue ,2007年6月5日23:44:57 + 0200,J Caesar写道:


在C中你可以比较两个指针p< q,只要它们来自

相同的数组或相同的malloc()ated块。否则你不能。



真的!?这些年来我怎么设法这样做了?


指针只是地址 - 通常是32-,这些天更多,

更多,64位无符号整数。你可以随意比较。

通常你会希望他们成为相同类型数据的指针,

课程。


>

我想做的是写一个类似的函数int(void * p,void * q)

将采取任何两个指针并决定是否可以比较




我真的不能怎么想这样做 - 有什么建议吗?


JC。




" J Caesar" < in ***** @ nospam.comwrote in message

news:sl ******************** @ nospam.com。 ..


在C中你可以比较两个指针,p< q,只要它们来自

相同的数组或相同malloc()吃了块。否则你就不能。


我想做的就是写一个函数

int comparative(void * p,void * q)

将需要任意两个指针并决定是否可以比较




我真的不能想想怎么做 - 任何建议?



这是不可能做到的。或者更确切地说,它不可能是便携式的。要做到这一点

非便携式可能要么是微不足道的,要么编译器没有问题

指向不同块的指针。


无比较规则是一个允许有趣的架构的kludge。当你需要知道指针是否来自

某个块时,
会变得很麻烦。但是你经常不需要这些信息,总而言之,最好让编译器更容易实现或者编码以便更快地运行。


-

免费游戏和编程好东西。
http://www.personal.leeds.ac.uk/~bgy1mm


< blockquote>

" Ivar Rosquist" < IR ******* @ irq.org写了留言

news:pa ********************* @ irq .org ...


On Tue,05 Jun 2007 23:44:57 +0200,J Caesar写道:


>在C中你可以比较两个指针p< q,只要它们来自同一个数组或相同的malloc()块。否则你不能。



真的!?这些年来我怎么设法这样做?



这是未定义的行为。实际上,平面存储器架构只需要进行明星向前减法。然而它可以陷阱或

在某些机器上返回有趣的结果。


-

免费游戏和编程好东西。 /> http://www.personal.leeds.ac.uk/~ bgy1mm


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.

解决方案

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.



"J Caesar" <in*****@nospam.comwrote in message
news:sl********************@nospam.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



"Ivar Rosquist" <IR*******@irq.orgwrote in message
news:pa*********************@irq.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 starightforwards 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


这篇关于比较指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆