473,750 Members | 2,209 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

one past the last element of an array object

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 ?

Dec 6 '06 #1
5 3691
<ju**********@y ahoo.co.inwrote in message
news:11******** **************@ j72g2000cwa.goo glegroups.com.. .
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++;
}

---------
Dave.

Dec 6 '06 #2
ju**********@ya hoo.co.in wrote:
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.

Dec 6 '06 #3
ju**********@ya hoo.co.in wrote:
>
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.

--
pete
Dec 6 '06 #4
pete wrote:
>
ju**********@ya hoo.co.in wrote:

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)
*/
}
I screwed that up by generating a "one before" pointer.
It should be:

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.
--
pete
Dec 6 '06 #5
"ju**********@y ahoo.co.in" <ju**********@y ahoo.co.inwrite s:
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.

--
Ben.
Dec 6 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2161
by: BigMan | last post by:
I wonder if the C++ standard says what should happen if I increment a pointer to a one-past-the-end value. I think it says that it is perfectly legal to increment a pointer to the last element element in an array - I get a pointer to one-past-the-end value. It is guaranteed that such a value exists for any array, so I always get a valid pointer to something (I do not care what it is). So, my question is what if I go further and increment...
11
1944
by: Sushil | last post by:
Hi Gurus I've tried to come up with a small logical example of my problem. The problem is platform specific (MIPS) which I understand should not be discussed here. So here goes my example: Code is doing malloc of variable sizes. The last byte of malloc.ed memory is written a magic.
3
3456
by: ozbear | last post by:
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
7
1692
by: Old Wolf | last post by:
For the following code: int main(void) { short a, *ptr; ptr = a + 100; ptr -= 0; }
5
2622
by: Mark Stijnman | last post by:
I have a question about forward iterators and what one should do or not do with them. I'm planning on writing a container that, when all boils down to it, stores a sequence of strings. I want threads to be able to read this sequence from start to end, and when they reach the end, wait until new data is added to the end. If so, it should pick up reading where it left off. The question is, is it valid and moral to do something like this from...
12
2047
by: spibou | last post by:
Why is a pointer allowed to point to one position past the end of an array but not to one position before the beginning of an array ? Is there any reason why the former is more useful than the later ? Spiros Bousbouras
3
1602
by: Miro | last post by:
First off...thanks in advance for getting me this far. Sorry for all these class posts but im having a heck of a time here trying to get something to work, and have finally got it to work ( yahooooo ) but i dont know why now I cant get it to work the other way. Vb 2003 Below are 2 examples. One Does not work and the other does.
12
3628
by: John Goche | last post by:
A lot of C++ code allocates a buffer and initializes start and end pointers as follows: +-------------------------------+ + + +-------------------------------+ ^ ^ | | pStart pEnd
14
1494
by: jois.de.vivre | last post by:
Hello, I was wondering if the following code was ok: // ----------- start code #include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, char** argv) {
0
8999
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8836
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9338
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9256
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8260
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6080
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4712
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4885
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2223
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.