Re: [PATCH] A new version of the sys_swapon bugfix

Stephen Williams (steve@icarus.com)
Tue, 03 Nov 1998 13:49:26 -0800


static int i = 0, *p;
if (&p[i - 1] >= p) printf("C does not promote in pointer arithmetic\n");

Out of ISO/IEC 9899-1990:
6.3.6 Additive Operators

Semantics

[...] If the pointer operand points to an element of an array object, and
the array is large enough, the result points to an element offset from the
original element such that the difference of the subscripts of the
resulting and original array elements equals the integral expression.

So if p + i == q, then q - p == i. If in addition "i < 0", the q must be
less then p. The only constraint on i is that it is an integral expression,
and the constraints on p and q are that they point into the same "array
object." Hell, ptrdiff_t (a signed integral type) may have fewer bits
then a pointer, and the algebra must certainly work there.

I'm pretty sure this applies to your example by noting the section:

6.3.2.1 Array Subscripting

Semantics
[...] The definition of the subscript operator [] is that E1[E2] is
identical to (*(E1+(E2))). Because if the conversion rules that apply
to the binary + operator, if E1 is an array object (equivalently, a
pointer to the initial element of an array object) and E2 is an integer,
E1[E2] designates the E2-th element of E1 (counting from zero).

So I would say that any compiler that generates code that prints your
message "C does not promote..." is broken, no matter how many bits in a
pointer.

-- 
Steve Williams                "The woods are lovely, dark and deep.
steve@icarus.com              But I have promises to keep,
steve@picturel.com            and lines to code before I sleep,
http://www.picturel.com       And lines to code before I sleep."

- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.rutgers.edu Please read the FAQ at http://www.tux.org/lkml/