Can an equality comparison of unrelated pointers evaluate to true?

前端 未结 4 844
野的像风
野的像风 2020-11-30 03:41

Section 6.5.9 of the C standard regarding the == and != operators states the following:

2 One of the following shal

4条回答
  •  再見小時候
    2020-11-30 04:31

    int a;
    int b;
    printf("a precedes b: %d\n", (&a + 1) == &b);
    printf("b precedes a: %d\n", (&b + 1) == &a);
    

    is perfectly well-defined code, but probably more by luck than by judgement.

    You are allowed to take the address of a scalar and set a pointer one past that address. So &a + 1 is valid, but &a + 2 is not. You are also allowed to compare the value of a pointer of the same type with the value of any other valid pointer using == and !=, although pointer arithmetic is only valid within arrays.

    Your assertion that the address of a and b tells you about anything about how these are placed in memory is bunk. To be clear, you cannot "reach" b by pointer arithmetic on the address of a.

    As for

    struct s {
        int a;
        int b;
    };
    

    The standard guarantees that the address of the struct is the same as the address of a, but an arbitrary amount of padding is allowed to be inserted between a and b. Again, you can't reach the address of b by any pointer arithmetic on the address of a.

提交回复
热议问题