ptr1 == ptr2但是(int)ptr1!=(int)ptr2 [英] ptr1 == ptr2 but (int)ptr1 != (int)ptr2

查看:80
本文介绍了ptr1 == ptr2但是(int)ptr1!=(int)ptr2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道这可能会破坏的架构吗?


T * ptr1,* ptr2;

...

if(ptr1 == ptr2)

if(CHAR_BIT * sizeof(T *)< =(int的宽度))/ *否则未定义* /

断言((int)ptr1 ==(int)ptr2);


(如果这有助于

打破某些东西,请随意用另一个整数类型替换int。 )


我知道一个addess至少可以在一些DOS

内存模型上有几个表示,但我不知道它是否在指针之前标准化了

转换为整数。


-

问候,

Hallvard

Do anyone know of an architecture where this can break?

T *ptr1, *ptr2;
...
if (ptr1 == ptr2)
if (CHAR_BIT*sizeof(T*) <= (width of int)) /*otherwise undefined*/
assert((int)ptr1 == (int)ptr2);

(Feel free to replace int with another integer type if that helps to
break something.)

I know an addess can have several representations at least on some DOS
memory models, but I don''t know if it normalizes pointers before
converting to integer.

--
Regards,
Hallvard

推荐答案

Hallvard B Furuseth写道:
Hallvard B Furuseth wrote:

>

有谁知道这可能会破坏的架构?


T * ptr1,* ptr2;

...

if(ptr1 == ptr2)

if(CHAR_BIT * sizeof(T *)< =(int of int))/ * otherwi se undefined * /

assert((int)ptr1 ==(int)ptr2);


(如果是这样,可以随意用另一个整数类型替换int帮助

打破一些东西。)


我知道一个addess至少可以在某些DOS上有几个表示

内存模型,但我不知道它是否在指向

转换为整数之前使指针正常化。
>
Do anyone know of an architecture where this can break?

T *ptr1, *ptr2;
...
if (ptr1 == ptr2)
if (CHAR_BIT*sizeof(T*) <= (width of int)) /*otherwise undefined*/
assert((int)ptr1 == (int)ptr2);

(Feel free to replace int with another integer type if that helps to
break something.)

I know an addess can have several representations at least on some DOS
memory models, but I don''t know if it normalizes pointers before
converting to integer.



我怀疑,在大多数平台上,如果指针比较相等,指针可以适用于
,如果指针比较相等,那么convert-to-int

值也将相等。


然而,我确定标准可能会说它的实现

充其量定义。

考虑分段存储器架构,例如实模式和实模式。

x86系列处理器。在这样的平台上远指针是

32位(16位段加上16位偏移量),我怀疑

可能两个指针可以比较相等,甚至
如果他们的位模式不相同,则为
。 (例如,它可能将
比较FFFF:0000和F000:FFF0作为相等,但是0xFFFF0000和

0xF000FFF0因为整数[或者可能是多头]不会比较相等。)


-

+ ------------------------ - + -------------------- + ----------------------- +

| Kenneth J. Brody | www.hvcomputer.com | #include |

| kenbrody / at\spamcop.net | www.fptech.com | < std_disclaimer.h |

+ ------------------------- + --------- ----------- + ----------------------- +

不要 - 邮寄给我:< mailto:Th ************* @ gmail.com>

I would suspect that, on most platforms in which a pointer can fit
into an int, if the pointers compare equal, then the converted-to-int
values will also compare equal.

However, I''m sure the standard probably says it''s implementation
defined at best.

Consider a segmented memory architecture, such as "real-mode" on
the x86 line of processors. On such platforms "far" pointers are
32 bits (16-bit segment, plus 16-bit offset), and I would suspect
that it may be possible for two pointers to compare equal, even
if their bit patterns are not identical. (For example, it may
compare FFFF:0000 and F000:FFF0 as "equal", but 0xFFFF0000 and
0xF000FFF0 as ints [or perhaps longs] will not compare equal.)

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don''t e-mail me at: <mailto:Th*************@gmail.com>




" Hallvard B Furuseth" < h。********** @ usit.uio.nowrote in message

news:hb ************** @ bombur。 uio.no ...

"Hallvard B Furuseth" <h.**********@usit.uio.nowrote in message
news:hb**************@bombur.uio.no...

有谁知道这可能会破坏的架构?


T * ptr1,* ptr2;

...

if(ptr1 == ptr2)

if(CHAR_BIT * sizeof(T *)< =(width of int))/ *否则未定义* /

断言((int)ptr1 ==(int)ptr2);


(随意替换int使用另一个整数类型,如果这有助于

打破一些东西。)


我知道一个addess至少可以在某些DOS上有几个表示

内存模型,但我不知道它是否在指向

转换为整数之前标准化指示。
Do anyone know of an architecture where this can break?

T *ptr1, *ptr2;
...
if (ptr1 == ptr2)
if (CHAR_BIT*sizeof(T*) <= (width of int)) /*otherwise undefined*/
assert((int)ptr1 == (int)ptr2);

(Feel free to replace int with another integer type if that helps to
break something.)

I know an addess can have several representations at least on some DOS
memory models, but I don''t know if it normalizes pointers before
converting to integer.



我会如果它确实会感到惊讶。如果有人想将指针转换为

整数,通常他们想要得到这些位,所以重新解释似乎很可能是b $ b。这意味着如果相同的地址有两种解释,那么它将会破坏。


然而实际上ptr1 == ptr2几乎肯定会破坏。那个

是为什么创建一个漫游超过一个

对象的指针是非法的。如果体系结构是分段的,那么生成两个不同指向同一物理地址的唯一C方式就是移动

超出其范围。因此,在运行时,程序可以通过共谋位来廉价地比较指针

的相等性,并且不需要标准化步骤。

-

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


Malcolm McLean写道:
Malcolm McLean wrote:

> ;

" Hallvard B Furuseth" < h。********** @ usit.uio.nowrote in message

news:hb ************** @ bombur。 uio.no ...
>
"Hallvard B Furuseth" <h.**********@usit.uio.nowrote in message
news:hb**************@bombur.uio.no...

>有人知道这可能会破坏的架构吗?

T * ptr1,* ptr2;
...
if(ptr1 == ptr2)
if(CHAR_BIT * sizeof(T *)< =(int的宽度))/ *否则未定义* /
assert((int)ptr1 ==(int)ptr2);

(如果有助于破坏某些东西,请随意用另一个整数类型替换int。)

我知道一个addess至少可以在一些DOS内存模型上有几个表示,但我不知道它是否在转换为整数之前规范化指针。
>Do anyone know of an architecture where this can break?

T *ptr1, *ptr2;
...
if (ptr1 == ptr2)
if (CHAR_BIT*sizeof(T*) <= (width of int)) /*otherwise undefined*/
assert((int)ptr1 == (int)ptr2);

(Feel free to replace int with another integer type if that helps to
break something.)

I know an addess can have several representations at least on some DOS
memory models, but I don''t know if it normalizes pointers before
converting to integer.



如果确实如此,我会感到惊讶。如果有人想将指针转换为

整数,通常他们想要得到这些位,所以重新解释似乎很可能是b $ b。这意味着如果相同的地址有两个

的解释,它就会破坏。


然而实际上ptr1 == ptr2几乎肯定会破坏。

I''d be surprised if it does. If someone wants to convert a pointer to an
integer normally they want to get at the bits, so reinterpretation seems
likely. That would mean that if the same address has two
interpretations, it would break.

However in reality ptr1 == ptr2 would almost certainly break as well.



否,ptr1 == ptr2由标准保证始终有效[1]是否

指针与指针有任何关系彼此是否或

不是他们对相同的值使用不同的表示。

No, ptr1==ptr2 is guaranteed by the standard to always work[1] whether
or not the pointers have any relationship to each other and whether or
not they use different representations for the same value.


这就是为什么创建指针是非法的漫游超过

一个对象。
That is why it is illegal to create a pointer that roams over more than
one object.



这是一个单独的问题,不同的表示是否可以用于相同指针值的
。它允许范围检查

实现,确实存在并且不需要

作为相同物理地址的不同表示。

That is a separate matter from whether different representations can be
used for the same pointer value. It allows for range checking
implementations, something which does exist and does not require there
to be different representations of the same physical address.


如果体系结构是分段的,那么生成两个不同指向同一物理地址的唯一C方式就是

移动一个以上它的范围。
If the architecture is segmented, probably the only C way of
generating two different pointers to the same physical address is to
move one beyond its range.



错误。该体系结构可以具有重叠的段,例如:

{

int arr [4069]; / * arr从段1开始* /

int * ptr1 = a + 1024; / * ptr1从段2开始* /

int * ptr2 = a + 4096; / * ptr2从段3开始* /

而(ptr1!= ptr2)ptr2--;

然后当ptr1使用段2但ptr2正在使用段时它们相遇3.

编译器必须使其工作。


旧的x86系列处理器使用重叠段,尽管

我不知道是否有任何编译器允许对象(或malloced区域)

大于段。

Wrong. The architecture could have overlapping segments such that:
{
int arr[4069]; /* arr starts in segment 1 */
int *ptr1 = a+1024; /* ptr1 starts in segment 2 */
int *ptr2 = a+4096; /* ptr2 starts in segment 3 */
while (ptr1 != ptr2) ptr2--;
Then they meet when ptr1 is using segment 2 but ptr2 is using segment 3.
The compiler has to make it work.

The good old x86 range of processors uses overlapping segments, although
I don''t know if any compilers allowed objects (or malloced regions)
larger than a segment.


因此在运行时程序通过共谋位可以便宜地比较

指针的相等性,并且不需要标准化

步骤。
So at runtime the program can cheaply compare
pointer for equality by comaring bits, and doesn''t need a normalisation
step.



错误。标准保证比较平等总是有效。

只有关系运算符排除了平等和不平等,

不必工作。


[1]如果其中一个指针既不是null,也不是指向对象的指针,

也不是指向1经过对象的指针,那么就会发生未定义的行为。

在比较之前评估指针。

-

Flash Gordon

Wrong. The standard guarantees that comparing for equality always works.
It is only relational operators excluding equality and inequality that
do not have to work.

[1] If one of the pointers is neither null, nor a pointer to an object,
nor a pointer to 1 past an object, then undefined behaviour occurs on
evaluating the pointer before you get as far as the comparison.
--
Flash Gordon


这篇关于ptr1 == ptr2但是(int)ptr1!=(int)ptr2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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