Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why two identical pointers do not compare equal with -O1? [duplicate]

#include <stdio.h>

int main(void)
{
    int a, b;
    int *p = &a;

#ifdef __clang__
    int *q = &b + 1;
#elif __GNUC__
    int *q = &b - 1;
#endif

    printf("%p %p %d\n", (void *)p, (void *)q, p == q);
}

C11 § 6.5.9 \ 6 says that

Two pointers compare equal if and only if both are null pointers, both are pointers to the same object (including a pointer to an object and a subobject at its beginning) or function, both are pointers to one past the last element of the same array object, or one is a pointer to one past the end of one array object and the other is a pointer to the start of a different array object that happens to immediately follow the first array object in the address space.

I have tested it four different ways:

  1. Clang 9.0.1 with -01 option;
  2. Clang 9.0.1 without any options;
  3. GCC 9.2.0 with -01 option;
  4. GCC 9.2.9 without any options.

The results are the following:

$ ./prog_clang
0x7ffebf0a65d4 0x7ffebf0a65d4 1
$ ./prog_clang_01
0x7ffd9931b9bc 0x7ffd9931b9bc 1
$ ./prog_gcc
0x7ffea055a980 0x7ffea055a980 1
$ ./prog_gcc_01
0x7fffd5fa5490 0x7fffd5fa5490 0

What is the correct behavior in this case?

like image 831
eanmos Avatar asked Nov 16 '25 07:11

eanmos


2 Answers

What is the correct behavior in this case?

There is none. Comparing pointers to or one past the end of two completely unrelated objects is undefined behavior.

Per footnote 109 of the C11 standard (bolding is mine):

Two objects may be adjacent in memory because they are adjacent elements of a larger array or adjacent members of a structure with no padding between them, or because the implementation chose to place them so, even though they are unrelated. If prior invalid pointer operations (such as accesses outside array bounds) produced undefined behavior, subsequent comparisons also produce undefined behavior.

like image 187
Andrew Henle Avatar answered Nov 19 '25 02:11

Andrew Henle


Two pointers compare equal if and only if both are null pointers,

they are not null

both are pointers to the same object (including a pointer to an object and a subobject at its beginning) or function

they do not point to the same object, nor a subobject, nor a function

both are pointers to one past the last element of the same array object,

they are not pointers to array elements.

or one is a pointer to one past the end of one array object and the other is a pointer to the start of a different array object that happens to immediately follow the first array object in the address space.

they are not pointers to array elements.


So, according to the standard, your pointers do not meet the requirements for comparing as equal, and should have never compared as equal.

Now, in your tests, in the first three cases, the pointers did in fact compare as equal. One can say that the compilers do not strictly adhere to the standard, because the standard says "if and only if", but as you have seen, clang and gcc without -O1 behave as if the standard said "if" without the "and only if" part. The compilers simply do not try to take extra measures to ensure that the "and only if" part is respected, so they allow the pointers to compare as equal, as a matter of pure coincidence, despite the fact that according to the standard, they shouldn't.

Since it was pure coincidence, in the last case the coincidence does not hold true anymore, due to a number of unknown reasons having to do with the compiler's implementation of optimizations. The compiler may have decided to reverse the order of the variables on the stack, or to put them farther away from each other, or who knows what.

like image 31
Mike Nakis Avatar answered Nov 19 '25 03:11

Mike Nakis