Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

== for pointer comparison

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

Any pointer can be meaningfully compared for equality or inequality with zero. But the behavior is undefined for arithmetic or comparisons with pointers that do not point to members of the same array. (There is one exception: the address of the first element past the end of an array can be used in pointer arithmetic.)

Does this mean I cannot rely on == for checking equality of different pointers? What are the situations in which this comparison leads to a wrong result?

like image 977
Manlio Avatar asked Apr 03 '12 13:04

Manlio


People also ask

Can you compare pointers with ==?

Pointers of the same type (after pointer conversions) can be compared for equality. Two pointers of the same type compare equal if and only if they are both null, both point to the same function, or both represent the same address (3.9.

How do I compare the contents of two pointers?

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 types both point to the same object, or both point one past the last element of the same array object, they compare equal.

Can we compare pointers in C?

In C language pointers can be compared if the two pointers are pointing to the same array. All relational operators can be used for pointer comparison, but a pointer cannot Multiplied or Divided.


4 Answers

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.

like image 164
Sergey Kalinichenko Avatar answered Oct 06 '22 18:10

Sergey Kalinichenko


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.

like image 33
R.. GitHub STOP HELPING ICE Avatar answered Oct 06 '22 16:10

R.. GitHub STOP HELPING ICE


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.

like image 28
glglgl Avatar answered Oct 06 '22 16:10

glglgl


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.

like image 23
Tony The Lion Avatar answered Oct 06 '22 17:10

Tony The Lion