469,270 Members | 1,232 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

One-past-end-of-object pointers

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
3 2948
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

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

Similar topics

18 posts views Thread by M. Clift | last post: by
1 post views Thread by Mike Rogers | last post: by
20 posts views Thread by Felix E. Klee | last post: by
9 posts views Thread by Javier | last post: by
5 posts views Thread by Francesc | last post: by
2 posts views Thread by dmh_test1 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.