By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,264 Members | 1,746 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,264 IT Pros & Developers. It's quick & easy.

One-past-end-of-object pointers

P: n/a
This is probably an obvious question.

I know that pointer comparisons are only defined if the two pointers
point somewhere "into" the storage allocated to the same object, or if
they are NULL, or one-past the end of the object as long as it isn't
dereferenced.

I use "object" in the standard 'C' sense.

Is there some special dispensation given to comparing two pointers
that are -each- one past the end of the object as opposed to
comparing a one-past-end pointer and an pointer expression derived
referencing the actual object. Given the following definitions:

char arr[100];
char *pend = &arr[99];
char *pa = pend+1;
char *pb = pend+1;

is the comparison pa == pb guaranteed to be meaningful or does it
invoke UB because -neither- pa nor pb point into arr.

In otherwords, from the standard's point of view, is there a
semantic difference between comparing pa and pb and comparing,
say, pa and &arr[99]+1 ?

Oz
--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 14 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
oz****@bigpond.com (ozbear) writes:
char arr[100];
char *pend = &arr[99];
char *pa = pend+1;
char *pb = pend+1;

is the comparison pa == pb guaranteed to be meaningful or does it
invoke UB because -neither- pa nor pb point into arr.


It is meaningful. pa and pb will compare equal. See 6.5.9#6:

Two pointers compare equal if and only if both are null
pointers, both are pointers to the same object (including a
pointer to an object and a subobject at its beginning) or
function, both are pointers to one past the last element of
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
the same array object, or one is a pointer to one past the
^^^^^^^^^^^^^^^^^^^^^
end of one array object and the other is a pointer to the
start of a different array object that happens to
immediately follow the first array object in the address
space.91)

(Note the final clause, which could cause a "false equal"
condition if the pointers point into different objects.)
--
"A lesson for us all: Even in trivia there are traps."
--Eric Sosman
Nov 14 '05 #2

P: n/a

"ozbear" <oz****@bigpond.com> wrote in message
news:41364c4d.687511734@news-server...
This is probably an obvious question.

I know that pointer comparisons are only defined if the two pointers
point somewhere "into" the storage allocated to the same object, or if
they are NULL, or one-past the end of the object as long as it isn't
dereferenced.

I use "object" in the standard 'C' sense.

Is there some special dispensation given to comparing two pointers
that are -each- one past the end of the object as opposed to
comparing a one-past-end pointer and an pointer expression derived
referencing the actual object. Given the following definitions:

char arr[100];
char *pend = &arr[99];
char *pa = pend+1;
char *pb = pend+1;

is the comparison pa == pb guaranteed to be meaningful or does it
invoke UB because -neither- pa nor pb point into arr.

In otherwords, from the standard's point of view, is there a
semantic difference between comparing pa and pb and comparing,
say, pa and &arr[99]+1 ?


====================== begin quote ====================================
ISO/IEC 9899:1999 (E)

[....]

6.5.8 Relational operators

[....]

5 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.

[....]

6.5.9 Equality operators

[....]

6 Two pointers compare equal if and only if both are null pointers, both
are pointers to the same object (including a pointer to an object and a
subobject at its beginning) or function, both are pointers to one past
the last element of the same array object, or one is a pointer to one
past the end of one array object and the other is a pointer to the start
of a different array object that happens to immediately follow the first
array object in the address space.(91)

[....]

(91) Two objects may be adjacent in memory because they are adjacent
elements of a larger array or adjacent members of a structure with
no padding between them, or because the implementation chose to
place them so, even though they are unrelated. If prior invalid
pointer operations (such as accesses outside array bounds) produced
undefined behavior, subsequent comparisons also produce undefined
behavior.
====================== end quote ====================================

-Mike
Nov 14 '05 #3

P: n/a
Ben Pfaff wrote:

oz****@bigpond.com (ozbear) writes:
char arr[100];
char *pend = &arr[99];
char *pa = pend+1;
char *pb = pend+1;

is the comparison pa == pb guaranteed to be meaningful or does it
invoke UB because -neither- pa nor pb point into arr.


It is meaningful. pa and pb will compare equal. See 6.5.9#6:

Two pointers compare equal if and only if both are null
pointers, both are pointers to the same object (including a
pointer to an object and a subobject at its beginning) or
function, both are pointers to one past the last element of
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
the same array object, or one is a pointer to one past the
^^^^^^^^^^^^^^^^^^^^^
end of one array object and the other is a pointer to the
start of a different array object that happens to
immediately follow the first array object in the address
space.91)

(Note the final clause, which could cause a "false equal"
condition if the pointers point into different objects.)


That's not a false equal.
If two objects are contigous in memory, then
the one past pointer from the first object,
really is equal to the start of the next object,
(and a single string could span both objects).

--
pete
Nov 14 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.