4 ms·
Note that if the two pointers are passed to a function, and the comparison is done in the function, the results are different: #include <stdio.h> void pcm
by tzs 8y ago
Note that if the two pointers are passed to a function, and the comparison is done in the function, the results are different:
#include <stdio.h>
void pcmp(int *p, int *q)
{
printf("%p %p %d\n", (void *)p, (void *)q, p == q);
}
int main(void) {
int a, b;
int *p = &a;
int *q = &b + 1;
printf("%p %p %d\n", (void *)p, (void *)q, p == q);
pcmp(p, q);
return 0;
}
That is giving me:
0x7ffebac1483c 0x7ffebac1483c 0
0x7ffebac1483c 0x7ffebac1483c 1
That's compiled with '-std=c11 -O1' as in the article. The result is the same of pcmp is moved into a separate file so that when compiling it the compiler has no knowledge of the origins of the two pointers.
I don't like this at all. It bugs me that I can get different results comparing two pointers depending on where I happen to do the comparison.
- JoeAltmaier 8y agoYes, I agree. It makes far more sense to make guarantees in a language, that two values are compared using a metric that is invariant. Especially in a language like C, where we expect it to be a very thin abstraction over the machine.
- deleted 8y ago[deleted]
- ash_gti 8y agoDid you use gcc or clang or...? When I ran this locally I get: 0x7ffeee2a4808 0x7ffeee2a4810 0 0x7ffeee2a4808 0x7ffeee2a4810 0 With -O0 I get 1/1 instead of 0/0 so this appears to be a compiler optimization.
- quelltext 8y agoYup, what is described in the article looks like a bugged optimization to me were the runtime behavior differs from the static model used for the optimization. I am sure the actual runtime semantics is in fact to compare addresses but the optimization claims to know (falsely) enough about the pointers to determine they must be not equal. I don't understand the people in this comment section defending this. The cited spec gives no evidence that this should happen (I fail to see the undefined behavior here). It specifies when pointers are equal, and both pointers ended up pointing to the same object, so they should be equal. Just because the compiler doesn't know, shouldn't mean it can replace an equality check with false. The spec didn't say "also, they are not equal in case the conpiler thinks that they proooobably aren't pointing to the same object". I do agree that one should avoid this type of pointer arithmetic, but that doesn't excuse the confusing behavior of this primitive operstion. One should at least argue for the behavior to be consistent.
- 0x417572656c 8y agoYou're right, unless pcmp is declared as static (then the compiler inline the function) #include <stdio.h> #include <stdint.h> static void pcmp(void *p, void *q) { printf("%p %p %d %d(sub) %d(cast)\n", p, q, p == q, !(p - q), (uintptr_t)p == (uintptr_t)q); } int main(void) { int a, b; void *p = &a; void *q = &b + 1; printf("%p %p %d %d(sub) %d(cast)\n", p, q, p == q, !(p - q), (uintptr_t)p == (uintptr_t)q); pcmp(p, q); return 0; } Results: $ gcc -std=gnu11 -O1 pq.c $ ./a.out 0x7fffde6612ec 0x7fffde6612ec 0 1(sub) 1(cast) 0x7fffde6612ec 0x7fffde6612ec 0 1(sub) 1(cast) Same result with '-std=gnu11' as with '-std=c11' (GNU C allow void pointer arithmetic for the test !(p - q))