== for pointer comparison

前端 未结 5 1125
终归单人心
终归单人心 2021-01-03 19:35

I quote from \"The C Programming Language\" by Kernighan & Ritchie:

Any pointer can be meaningfully compared for equality or inequality with zero.

相关标签:
5条回答
  • 2021-01-03 19:40

    The equality operator is defined for all valid pointers, and the only time it can give a "false positive" is when one pointer points to one element past the end of an array, and the other happens to point (or points by virtue of a structure definition) to another object stored just past the array in memory.

    I think your mistake is treating K&R as normative. See the C99 standard (nice html version here: http://port70.net/~nsz/c/c99/n1256.html), 6.5.9 on the equality operator. The issue about comparisons being undefined only applies to relational operators (see 6.5.8):

    When two pointers are compared, the result depends on the relative locations in the address space of the objects pointed to. If two pointers to object or incomplete types both point to the same object, or both point one past the last element of the same array object, they compare equal. If the objects pointed to are members of the same aggregate object, pointers to structure members declared later compare greater than pointers to members declared earlier in the structure, and pointers to array elements with larger subscript values compare greater than pointers to elements of the same array with lower subscript values. All pointers to members of the same union object compare equal. If the expression P points to an element of an array object and the expression Q points to the last element of the same array object, the pointer expression Q+1 compares greater than P. In all other cases, the behavior is undefined.

    0 讨论(0)
  • 2021-01-03 19:46

    One example that comes to my mind is Harvard architecture with separate address spaces for code and for data. In computers of that architecture the compiler can store constant data in the code memory. Since the two address spaces are separate, a pointer to an address in the code memory could be numerically equal to a pointer in the data memory, without pointing to the same address.

    0 讨论(0)
  • 2021-01-03 19:46

    I interpret this as following:

    short a[9];
    int b[12];
    short * c = a + 9;
    

    Here it is valid to say that

    c > a
    

    because c results from a via pointer arithmetic,

    but not necessarily that

    b == c
    

    or

    c <= b
    

    or something alike, because they result from different arrays, whose order and alignment in memory is not defined.

    0 讨论(0)
  • 2021-01-03 19:55

    You cannot use pointer comparison for comparing pointers that point into different arrays.

    So:

    int arr[5] = {1, 2, 3, 4, 5};

    int * p = &arr[0];

    int anotherarr[] = {1, 2};

    int * pf = &anotherarr[0];

    You cannot do if (p == pf) since p and pf do not point into the same array. This will lead to undefined behaviour.

    You can rely on pointer comparison if they point within the same array.

    Not sure about the arithmetic case myself.

    0 讨论(0)
  • 2021-01-03 19:58

    You can do == and != with pointers from different arrays.

    <, <=, >, >= is not defined.

    0 讨论(0)
提交回复
热议问题