This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Do all arrays have to have a pad byte after them?


Alan Lehotsky wrote:
> At 08:54 -0500 11/14/98, Nathan Sidwell wrote:
>  * Hyman Rosen wrote:
>  * > I repeat the quote from the standard, section 5.10 -
>  * >
>  * >    Two pointers of the same type compare equal if and only if they are
>  * >    both null, both point to the same object or function, or both point
>  * >    one past the end of the same array.
>  * Your analysis assumes that a pointer cannot simultaneously point to one
>  * past the end of one array and the beginning of another array. I don't
>  * believe the standard prohibits that.
>         Ahh,... what do you think "if and only if" means....
> 
>         The standard is quite clear (and IMHO "broken") about
>         what it's saying here.....
The standard is possibly badly worded here, (but the intent is to not
require such padding).

I read `if and only if' as saying that when two pointers compare equal,
at least one of the following will be true,
1) both are null
2) both point to the same object
3) both point to one past the end of an array

It does not say these are mutually exclusive conditions, but it would be
rather silly, if one of them was implied by another, but that is not
what I'm asserting.
This part of the standard is not specifying that a pointer can be
exactly one of the categories {null, point to an object, point to one
past the end of an array, something else}. 

Of course elsewhere, the standard says a pointer cannot be both null and
point to an object, or null and point to one past the end of an array.
(null pointers are guaranteed to compare unequal to a pointer to any
object or function).

The Rationale (http://www.lysator.liu.se/c/rat/c3.html#3-3-6) explicitly
tells us that it is always safe to calculate the address of just past
any object (even if the object is not an array). It says,

	An important endorsement of widespread practice is the requirement
	that a pointer can always be incremented to just past the end of an
	array, with no fear of overflow or wraparound: 

It then goes on to explain the minimum implementation requirements this
implies,
	This stipulation merely requires that every object be followed by
	one byte whose address is representable. That byte can be the first
	byte of the next object declared for all but the last object located
	in a contiguous segment of memory.  (In the example, the address
	&array[SPAN] must address a byte following the highest element of
	array.)  Since the pointer expression p+1 need not (and should not)
	be dereferenced, it is unnecessary to leave room for a complete
	object of size sizeof(*p).
Now, the rationale is not the standard, but everything in the rationale
is provable from the standard (isn't it?)

nathan
-- 
Dr Nathan Sidwell :: Computer Science Department :: Bristol University
      You can up the bandwidth, but you can't up the speed of light      
nathan@acm.org  http://www.cs.bris.ac.uk/~nathan/  nathan@cs.bris.ac.uk


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]