Search code examples
cpointerslanguage-lawyervoid-pointers

Is the equality relation of pointers after round-trip conversion guaranteed to be transitive?


This question is similar to "Are all pointers guaranteed to round-trip through void * correctly?" but slightly deeper.

Given:

#include <stdint.h>
int i;
int *ip1 = &i;
void *vp1 = ip1;
intptr_t x = (intptr_t)vp1;
void *vp2 = (void *)x;
int *ip2 = vp2;

then vp1 == vp2 is guaranteed to be true (even though they might not share the same binary representation), but is ip1 == ip2 guaranteed to be true? I.e., is the equality relation transitive in this case?


Solution

  • Transitivity of equality for pointers, regardless of provenance, follows from the specification of the equality operators. C 2018 6.5.9 6 says:

    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.

    Dismissing null pointers and pointers to functions, which are not an issue here, given that a == b and b == c evaluate as true, they must satisfy one of the conditions listed in the specification, so we have these cases:

    Given a == b. Given b == c. a == c?
    a and b both point to the same object . b and c both point to the same object. a and c both point to the same object. Therefore a == c evaluates as true.
    a and b both point to the same object. c points to one past the last element of an array object and b points to the start of an array object that happens to follow it. c points to one past the last element of an array object and a points to the start of an array object that happens to follow it. Therefore a == c evaluates as true.
    a and b both point to one past the last element of the same array object. b and c both point to one past the last element of the same array object. a and c both point to one past the last element of the same array object. Therefore a == c evaluates as true.
    a points to one past the last element of an array object and b points to the start of an array object that follows it. b and c both point to the same object. a points to one past the last element of an array object and c points to the start of an array object that follows it. Therefore a == c evaluates as true.
    b points to one past the last element of an array object and a points to the start of an array object that follows it. b and c both point to one past the last element of an array object. c points to one past the last element of an array object and a points to the start of an array object that follows it. Therefore a == c evaluates as true.

    Note there are no cases where b points to an object in the first column and to one past the last element of an array in the second column or vice-versa: Whichever of these two kinds of pointer it is, it must be the same kind in a == b and b == c.