473,218 Members | 1,477 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,218 software developers and data experts.

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 3385
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: M. Clift | last post by:
Hi, Just a quick one. I'm trying to call with this, but I keep getting 'unhashable'. I've tried various ' " and nextName = {:,\ 'Rita':],\ 'Sue':],\ 'Mary':}
1
by: Henry | last post by:
I have two tables, one is Student, which has studentid, personid, grade, fees as its columns. The other one is Person, which has personid, firstname, lastname, address as its columns. When given...
1
by: Mike Rogers | last post by:
trying to make a script to view data in a excell spreadsheet. I can get all the data I need except one. select ordernumber, itemcode, quantity, rate, total from dbo.charges where...
0
by: Fendi Baba | last post by:
Hi everyone I am new in this so please bear with me. I am trying to create a schema for a Quotation document. I created the creating the forms using Microsoft Infopath. I am creating my own...
20
by: Felix E. Klee | last post by:
Hi, when I compile the program below with gcc -O0 -o ctest ctest.c (Platform: Intel Celeron (Coppermine), LINUX 2.4.20) I get the following output: one != one_alt one_alt != one 1. !=...
9
by: Javier | last post by:
Hi I've a table with 2 columns, one for a client code and one for a date/time and could be more than one record with the same client code and date/time. the 3rd column is another date/time, NULL...
5
by: Francesc | last post by:
Hi, I'm programming some classes in order to be able to import different text formats (TXT, RTF, HTML, ...) to my own format called RUF. I've defined one Solution within several Projects, each...
2
by: dmh_test1 | last post by:
Hi, for the first time in my career I have a legitimate need for a one- to-one relationship between tables. I see no way to set op a one-to- one within an ADP project, however. Do I need to write...
8
by: anandmms | last post by:
Hello friend, my problem is to save more than one fingerprint images in one byte array and then store in database(oracle). i dont know how to solve it. But previously i had did image saving...
4
by: CatchSandeepVaid | last post by:
I have one-to-one relationship betnwee Product and ProductBasic. <one-to-one name="productBasic" cascade="save-update" property-ref="activeProduct" lazy="proxy"> <formula>'1'</formula> ...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.