简体   繁体   中英

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? 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 ).

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. 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.

Dereferencing x has undefined behavior; (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. But if *x happens to "work" and yield a value, it's also not surprising that *x == *y is true. It's possible that the compiler recognized that x == y and therefore concluded that *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 . Stands to reason that this simple address is the same, and that whatever data is actually AT that address will also be the same.

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. 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.

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

No. It invokes undefined behavior. You may get anything. Either this evaluates to true or false

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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