Does C or C++ guarantee array < array + SIZE? Does C or C++ guarantee array < array + SIZE? c c

Does C or C++ guarantee array < array + SIZE?


Yes. From section 6.5.8 para 5.

If the expression P points to an element of an array objectand the expression Q points to the last element of the same arrayobject, the pointer expression Q+1 compares greater than P.

Expression array is P. The expression array + SIZE - 1 points to the last element of array, which is Q.Thus:

array + SIZE = array + SIZE - 1 + 1 = Q + 1 > P = array


C requires this. Section 6.5.8 para 5 says:

pointers to array elements with larger subscript values compare greater than pointers to elements of the same array with lower subscript values

I'm sure there's something analogous in the C++ specification.

This requirement effectively prevents allocating objects that wrap around the address space on common hardware, because it would be impractical to implement all the bookkeeping necessary to implement the relational operator efficiently.


The guarantee does not hold for the case int *array = new(int[SIZE]); when SIZE is zero .

The result of new int[0] is required to be a valid pointer that can have 0 added to it , but array == array + SIZE in this case, and a strictly less-than test will yield false.