3
    int i = 0, j = 0;

int *a , *b;

a = &i + 1;
b = &j;

printf(" %p \n ",a);
printf(" %p \n ",b);

if(a==b)
printf(" yo \n");

Output:

0x7ffda8e133a4

0x7ffda8e133a4

a is same as b, still printf does not execute.

ideone link to this code

This works well, when I make a and b as normal variables. (int a, b;)

Anup Buchke
  • 5,210
  • 5
  • 22
  • 38
  • Can you please elaborate more? does this violate any C standard? – Anup Buchke May 08 '19 at 02:28
  • 6
    Comparing pointers that are not pointing to elements of the same array (or one past the end of it) is undefined. Check your output assembly code for what's going on specifically in your case. – Carl Norum May 08 '19 at 02:29
  • *undefined behavior* means *behavior that is not defined*, which means there is no standard for what will happen. In addition, you're using two uninitialized variables (`int i, j`). – Ken White May 08 '19 at 02:29
  • this doesn't work with initialized variables as well. But works when I make a and b as normal variables. Is this strict aliasing issue? – Anup Buchke May 08 '19 at 02:34
  • The last version of the code works for me! – OmG May 08 '19 at 02:36
  • It doesn't matter if `i` and `j` are initialized or not. It is guaranteed that they exist because their addresses are taken. – Swordfish May 08 '19 at 02:36
  • The result is `140725649638612 140725649638612 yo`. – OmG May 08 '19 at 02:37
  • 1
    I think compiler is doing a compile time optimization and removing this piece of code. – Anup Buchke May 08 '19 at 02:38
  • 1
    @tkausl No it isn't. Any valid pointers (which includes one-past-the-end) may be equality-compared. That is true in both C and C++. There is no rule that they should point to the same object (Carl Norum is confusing the rules for relational comparison with the rules for equality comparison) – M.M May 08 '19 at 03:26
  • In my opinion, gcc is behaving inconsistently, and is violating the standard. The gcc maintainers disagree. Follow the link to the duplicate question, and from there to the two GCC Bugzilla tickets. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63611 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61502 – Keith Thompson May 08 '19 at 03:33
  • @tkausl There's no such rule. In C++ you can also compare arbitrary pointers for equality, but the result is unspecified for the case in this question – M.M May 08 '19 at 03:50

1 Answers1

4

ISO/IEC 9899:201x

§6.5.6 Equality operators:

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

but then again:

  1. For the purposes of these operators, a pointer to an object that is not an element of an array behaves the same as a pointer to the first element of an array of length one with the type of the object as its element type.

So ... a and b should behave the same as a pointer to an array and i and j happen to be next to each other in memory ... then &i + 1 could point to the beginning of the first "array element" of j (b). Well, that's a big if. I wouldn't rely on that. Maybe in packed structs.


@OmG

The last version of the code works for me! [with initialized i and j]

gcc 9.1 -O3, 2 x printf() then ret.

        sub     rsp, 24
        mov     edi, OFFSET FLAT:.LC0
        xor     eax, eax
        lea     rsi, [rsp+12]
        mov     DWORD PTR [rsp+8], 0
        mov     DWORD PTR [rsp+12], 0
        call    printf                       ; <----------
        lea     rsi, [rsp+12]
        mov     edi, OFFSET FLAT:.LC0
        xor     eax, eax
        call    printf                       ; <----------
        xor     eax, eax
        add     rsp, 24
        ret                                  ; <----------

godbolt Compiler Explorer, gcc 9.1

Swordfish
  • 12,971
  • 3
  • 21
  • 43