one past the last element of an array object

J

junky_fellow

Hi,

I discussed about this earlier as well but I never got any
satisfactory answer. So, I am initiating this again.

Page 84, WG14/N869

"If both the pointer operand and the result point to elements of the
same array object, or one past the last element of the array object,
the evaluation shall not produce an overflow; otherwise, the
behavior is undefined."

First of all what is the meaning of "overflow" here ?
I wanted to know, what is so speacial about the element one past last
element of the array object. What is the reason that pointer pointing
to element one past the last element will not produce overflow ? I mean
to say, what was the need that the standard required "one past last
element" to be always a valid pointer ?
 
D

David T. Ashley

Hi,

I discussed about this earlier as well but I never got any
satisfactory answer. So, I am initiating this again.

Page 84, WG14/N869

"If both the pointer operand and the result point to elements of the
same array object, or one past the last element of the array object,
the evaluation shall not produce an overflow; otherwise, the
behavior is undefined."

First of all what is the meaning of "overflow" here ?
I wanted to know, what is so speacial about the element one past last
element of the array object. What is the reason that pointer pointing
to element one past the last element will not produce overflow ? I mean
to say, what was the need that the standard required "one past last
element" to be always a valid pointer ?

char ca[10];

char *p1, *p2;

p1 = ca + 9;

p2 = ca;

while (p2 <= p1) /* At the point this test fails, p2 is "one past" */
{
*p2 = 'x';
p2++;
}
 
S

santosh

Hi,

I discussed about this earlier as well but I never got any
satisfactory answer. So, I am initiating this again.

Page 84, WG14/N869

"If both the pointer operand and the result point to elements of the
same array object, or one past the last element of the array object,
the evaluation shall not produce an overflow; otherwise, the
behavior is undefined."
[ ... ] I mean
to say, what was the need that the standard required "one past last
element" to be always a valid pointer ?

Allowing a pointer to legally point to one element past the end of an
array enables idiomatic C constructs to avoid triggering undefined
behaviour.

Example:
for(ptr = start_ptr; ptr <= end_ptr; ptr++) /* do something */

When the loop above breaks, ptr points to one past the end of the array.
 
P

pete

Hi,

I discussed about this earlier as well but I never got any
satisfactory answer. So, I am initiating this again.

Page 84, WG14/N869

"If both the pointer operand and the result point to elements of the
same array object, or one past the last element of the array object,
the evaluation shall not produce an overflow; otherwise, the
behavior is undefined."

First of all what is the meaning of "overflow" here ?

Unmdefined Behavior.
I wanted to know, what is so speacial about the element one past last
element of the array object.

It comes in handy.

unsigned char *pointer = (unsigned char *)&object + sizeof object;

while (pointer-- != &object) {
/*
** do something with (*pointer)
*/
}
What is the reason that pointer pointing
to element one past the last element will not produce overflow ?
I mean to say, what was the need that the standard required
"one past last element" to be always a valid pointer ?

Besides being handy, it's not that hard to implement.

((char *)&object + sizeof object) is only byte past the object.

Contrast that with the "one before" pointer,
((char *)&object - sizeof object),
which is (sizeof object) bytes before the object
and (sizeof object) can be a big number.
 
P

pete

pete said:
Unmdefined Behavior.


It comes in handy.

unsigned char *pointer = (unsigned char *)&object + sizeof object;

while (pointer-- != &object) {
/*
** do something with (*pointer)
*/
}

I screwed that up by generating a "one before" pointer.
It should be:

while (pointer != &object) {
/*
** do something with (*--pointer)
*/
}
 
B

Ben Bacarisse

Hi,

I discussed about this earlier as well but I never got any
satisfactory answer. So, I am initiating this again.

Page 84, WG14/N869

"If both the pointer operand and the result point to elements of the
same array object, or one past the last element of the array object,
the evaluation shall not produce an overflow; otherwise, the
behavior is undefined."

First of all what is the meaning of "overflow" here ?
I wanted to know, what is so speacial about the element one past last
element of the array object. What is the reason that pointer pointing
to element one past the last element will not produce overflow ? I mean
to say, what was the need that the standard required "one past last
element" to be always a valid pointer ?

It may help to be a bit more explicit than some of the answers you
have had so far.

The text you quote is about a special dispensation regarding the
result of arithmetic on pointers.

An overflow occurs when the result of an arithmetic operation can not
be represented in the result type (usually because the result is "too
big"[1] or has "wrapped round"). The quoted text states that this
will not occur (i.e. the result will be representable) even when it
denotes a pointer one past the end of an array object.

Note, that it does not mean the such a pointer can be dereferenced.
Pretty much all you can do with it is compare it and subtract it (to
and from) other pointers to the same object.

[1] In "scare" quotes because it is hard to define such a terms since
the standard does not permit comparison of invalid pointers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top