ANSI C clarifications, with citations (was Re: [patch-2.4.0-test2]Re: Linux-2.4.0-test2)

From: Ben Pfaff (pfaffben@msu.edu)
Date: Sat Jun 24 2000 - 12:18:40 EST


There is a lot of misinformation floating around here. I feel
compelled to speak up.

#1: A compiler may insert padding between members of a struct or
after the last member. It may not insert padding before the
first member. Thus, the address of a struct member, plus one, is
not necessarily the address of the next member.

Citation: ANSI C89, section 6.5.2.1 "Structure and union
specifiers", as follows:

        Within a structure object, the non-bit-field members and
        units in which bit-fields reside have addresses that
        increase in the order in which they are declared. A
        pointer to a structure object, suitably converted, points
        to its initial member... and vice versa. There may
        therefore be unnamed padding within a structure object,
        but not at its beginning, as necessary to achieve the
        appropriate alignment.

#2: Pointer arithmetic takes into account the size of the object
pointed to. That is, ptr + 1 points to the same address as
((char *) ptr) + sizeof *ptr.

Citation: ANSI C89, section 6.3.6 "Additive operators", as
follows:

        When an expression that has integral type is added to or
        subtracted from a pointer, the result has the type of the
        pointer operand. 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.

I hope that cleared that up.

-
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/



This archive was generated by hypermail 2b29 : Mon Jun 26 2000 - 21:00:04 EST