简体   繁体   English

为什么两个相同值的整数指针指向同一个东西(via ==)?

[英]Why are two integer pointers of the same value point to the same thing (via ==) ?

int *x = 3;
int *y = 3;

if (x == y) "this statement evaluates to true" (pointer equality statement)
if (*x == *y) "this statement evaluates to true"

Is the reason that the pointer equality statement becomes true, just purely because COMPILER saw two "static" numbers "3" and said, hey point this to the same place? 是指针等式语句变为真的原因,仅仅是因为COMPILER看到两个“静态”数字“3”并且说,嘿指向同一个地方? Or is there some magic for integers in general. 或者一般来说整数有一些魔力。

It obviously seems redundant that deferencing an integer pointer is the same (in this case) as not dereferencing. 显然,冗余引用整数指针与未解除引用相同(在本例中)。

I've seen some examples of this problem pertaining to strings ( Addresses of two pointers are same ), but wanted to clarify it more. 我已经看到了一些与字符串相关的问题的例子( 两个指针的地址是相同的 ),但是想要澄清它。

int *x = 3;

This is invalid (a constraint violation ), and a conforming compiler is required to issue a diagnostic, and may reject it altogether. 这是无效的( 违反约束 ),并且需要符合标准的编译器来发出诊断,并且可以完全拒绝它。 You cannot use an integer value to initialize a pointer (except for the special case of 0 , which is a null pointer constant ). 您不能使用整数值来初始化指针(除了0的特殊情况,这是一个空指针常量 )。

If a compiler happens to accept it it will probably treat it as equivalent to: 如果编译器碰巧接受它,它可能会将其视为等效于:

int *x = (int*)3;

which causes the pointer x to point to address 3 in memory. 这导致指针x指向内存中的地址3 This is almost certainly nonsensical. 这几乎肯定是荒谬的。

Given that x and y are initialized with the same expression (and assuming your code isn't rejected), it's not at all surprising that x == y is true. 鉴于xy使用相同的表达式进行初始化(假设您的代码未被拒绝), x == y为真并不奇怪。

Dereferencing x has undefined behavior; 取消引用x具有未定义的行为; (int*)3 is very probably not a valid address, because it's outside your program's legal addressing space and/or because it's misaligned. (int*)3很可能不是有效地址,因为它超出了程序的合法寻址空间和/或因为它未对齐。 But if *x happens to "work" and yield a value, it's also not surprising that *x == *y is true. 但是如果 *x碰巧“工作”并产生一个值,那么*x == *y也是真的也就不足为奇了。 It's possible that the compiler recognized that x == y and therefore concluded that *x == *y . 编译器可能认识到x == y ,因此得出*x == *y结论。 You might determine that by examining the generated code. 您可以通过检查生成的代码来确定。 But it really doesn't matter; 但这真的没关系; once your program's behavior is undefined, quite literally anything can happen (or rather, the language standard permits literally anything to happen; the laws of physics might have something else to say about it). 一旦你的程序的行为未定义,完全可以发生任何事情(或者更确切地说,语言标准允许任何事情发生;物理定律可能还有别的东西可以说)。

You should have gotten a warning for both declarations. 您应该已经收到两个声明的警告。 If you did, you should heed it. 如果你这样做,你应该留意它。 If you didn't, you should find out how to increase the warning levels for your compiler. 如果没有,您应该了解如何增加编译器的警告级别。

You've simply hardcoded a fixed memory address for both pointers, 3 . 你只需要为两个指针硬编码一个固定的内存地址, 3 Stands to reason that this simple address is the same, and that whatever data is actually AT that address will also be the same. 有理由认为这个简单的地址是相同的,并且无论数据实际上是哪个AT地址都是相同的。

It's like you've got two sticky notes on your fridge. 这就像你的冰箱上有两个粘滞便笺。 Both says "keys are on table by door". 两人都说“钥匙在门上”。 And by jove, when you go look by the door, there's your keys. 通过jove,当你去门口看时,有你的钥匙。 Two different pointers, both pointing at the same thing. 两个不同的指针,都指向同一件事。

Both of the declaration 这两个声明

int *x = 3;
int *y = 3;

are constraint violation. 是约束违规。 You can declare 你可以申报

int *x = (int *)3;
int *y = (int *)3;

these both are referring to the address 3 of memory. 这两者都指的是记忆的地址3。

if (*x == *y) "this statement evaluates to true"  

No. It invokes undefined behavior. 不。它调用未定义的行为。 You may get anything. 你可能得到任何东西。 Either this evaluates to true or false 要么评估为truefalse

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM