473,327 Members | 2,074 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,327 software developers and data experts.

Incrementing a pointer to a one-past-the-end value?

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 a pointer to a
one-past-the-end value? Do I still get a valid pointer to anything?
Let me illustrate my question with a piece of code:

int main
(
)
{
int a[ 2 ];
int* p = a;
++p; // OK, points to the second element (indexed 1).
++p; // OK, points to one-past-the-end value.
++p; // What happens here? Is p a valid pointer? And what if the array
was allocated dynamically?
}

Jul 27 '05 #1
8 2125
BigMan wrote in
news:11**********************@g14g2000cwa.googlegr oups.com in
comp.lang.c++:
So, my question is what if I go further and increment a pointer to a
one-past-the-end value? Do I still get a valid pointer to anything?
Let me illustrate my question with a piece of code:

int main
(
)
{
int a[ 2 ];
int* p = a;
++p; // OK, points to the second element (indexed 1).
++p; // OK, points to one-past-the-end value.
++p; // What happens here? Is p a valid pointer? And what if the
array
was allocated dynamically?
You have undefined behaviour, IOW the standard no longer defines what
will or will not happen, it could be Very Bad Things(tm) or nothing
at all.
}


HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 27 '05 #2
I think this is enough. Thanks!

Jul 27 '05 #3
Rob Williscroft wrote:
BigMan wrote in
news:11**********************@g14g2000cwa.googlegr oups.com in
comp.lang.c++:

So, my question is what if I go further and increment a pointer to a
one-past-the-end value? Do I still get a valid pointer to anything?
Let me illustrate my question with a piece of code:

int main
(
)
{
int a[ 2 ];
int* p = a;
++p; // OK, points to the second element (indexed 1).
++p; // OK, points to one-past-the-end value.
++p; // What happens here? Is p a valid pointer? And what if the
array
was allocated dynamically?

You have undefined behaviour, IOW the standard no longer defines what
will or will not happen, it could be Very Bad Things(tm) or nothing
at all.


Surely he only has undefined behaviour if he actually dereferences it?

Ben
--
I'm not just a number. To many, I'm known as a String...
Jul 27 '05 #4
Ben Pope wrote in news:1122484994.3755e373a8cc924c5c567c3e34651725@t eranews
in comp.lang.c++:
Rob Williscroft wrote:
BigMan wrote in
news:11**********************@g14g2000cwa.googlegr oups.com in
comp.lang.c++:

So, my question is what if I go further and increment a pointer to a
one-past-the-end value? Do I still get a valid pointer to anything?
Let me illustrate my question with a piece of code:

int main
(
)
{
int a[ 2 ];
int* p = a;
++p; // OK, points to the second element (indexed 1).
++p; // OK, points to one-past-the-end value.
++p; // What happens here? Is p a valid pointer? And what if the
array
was allocated dynamically?

You have undefined behaviour, IOW the standard no longer defines what
will or will not happen, it could be Very Bad Things(tm) or nothing
at all.


Surely he only has undefined behaviour if he actually dereferences it?


If it isn't undefined, then surely it must be defined, so what does it
do ?

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 27 '05 #5
Ben Pope wrote:
Rob Williscroft wrote:
BigMan wrote:

int main
(
)
That's a new indenting style..
{
int a[ 2 ];
int* p = a;
++p; // OK, points to the second element (indexed 1).
++p; // OK, points to one-past-the-end value.
++p; // What happens here?

You have undefined behaviour, IOW the standard no longer defines what
will or will not happen, it could be Very Bad Things(tm) or nothing
at all.


Surely he only has undefined behaviour if he actually dereferences it?


No, the increment itself causes undefined behaviour.

As a rationale, consider these possibilities:
- a[] is the last object in memory (or the current segment)
- a+3 would be in a different process's address space

These possibilities are unlikely on an IA32 system with 4 Gigabytes
of RAM, but they are much more real on an embedded system.

Jul 27 '05 #6
Rob Williscroft wrote:
Ben Pope wrote in news:1122484994.3755e373a8cc924c5c567c3e34651725@t eranews
in comp.lang.c++:

Rob Williscroft wrote:
BigMan wrote in
news:11**********************@g14g2000cwa.googl egroups.com in
comp.lang.c++:

So, my question is what if I go further and increment a pointer to a
one-past-the-end value? Do I still get a valid pointer to anything?
Let me illustrate my question with a piece of code:

int main
(
)
{
int a[ 2 ];
int* p = a;
++p; // OK, points to the second element (indexed 1).
++p; // OK, points to one-past-the-end value.
++p; // What happens here? Is p a valid pointer? And what if the
array
was allocated dynamically?
You have undefined behaviour, IOW the standard no longer defines what
will or will not happen, it could be Very Bad Things(tm) or nothing
at all.


Surely he only has undefined behaviour if he actually dereferences it?

If it isn't undefined, then surely it must be defined, so what does it
do ?


I guessed the pointer was equal to a + 3*sizeof(int), regardless of what is or isn't there, but presumably it doesn't, which is why dereferencing it is bad.

presumably thats why:
std::vector<int> v;
std::vector<int>::iterator i = v.begin();
i--;
i++;

Isn't guaranteed to point to *v.begin()?

Ben
--
I'm not just a number. To many, I'm known as a String...
Jul 28 '05 #7
Ben Pope wrote in
news:1122540209.1629051a7bcb71987b0faeff0f5fb4f8@t eranews in
comp.lang.c++:
> int* p = a;
> ++p; // OK, points to the second element (indexed 1).
> ++p; // OK, points to one-past-the-end value.
> ++p; // What happens here? Is p a valid pointer? And what if
> the array
>was allocated dynamically? Surely he only has undefined behaviour if he actually dereferences
it?

If it isn't undefined, then surely it must be defined, so what does
it do ?


I guessed the pointer was equal to a + 3*sizeof(int), regardless of
what is or isn't there, but presumably it doesn't, which is why
dereferencing it is bad.


There are implementations where you have ((p + n) - n) == p, for
any non-void pointer p and any int n. There are other implementations
where a pointer (in a register) that doesn't actually point to valid
memory causes some kind of fault.

On such an implementaion the call (new int[10]) will need to allocate
11 int's just to make sure the one-passed-the-end rule is valid.

All implemetations need to avoid allocating the top of memory as
the last element of an array, as otherwise the requirment:

some_type array[N];
assert( (array + N) > (array + 0) );

i.e. the one-passed-the-end rule, will fail.
The point I'm trying to make is that undefined behaviour has
a plus side, the language is effeciently portable to a greater
number of platforms becuase of it.

For example, if the language allowed arbitary additions and
subtractions from pointers then platforms with checked pointer
registers would need to store and do all there arithmatic in
non-pointer registers, and then only load these values into the
pointer registers when dereferencing. This is against the
"Don't pay for what you don't use", "Spirit of C" that C++ inherited.
presumably thats why:
std::vector<int> v;
std::vector<int>::iterator i = v.begin();
Undefined Behaviour starts here:
i--;
All bets are now off.
i++;

Isn't guaranteed to point to *v.begin()?


Its worse than that, there is nolonger anything gauranteed.

undefined-behaviour,-it-really-does-what-it-says-on-the-tin-ly yr's Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 28 '05 #8
Rob Williscroft wrote:
There are implementations where you have ((p + n) - n) == p, for
any non-void pointer p and any int n. There are other implementations
where a pointer (in a register) that doesn't actually point to valid
memory causes some kind of fault. <SNIP> The point I'm trying to make is that undefined behaviour has
a plus side, the language is effeciently portable to a greater
number of platforms becuase of it.

For example, if the language allowed arbitary additions and
subtractions from pointers then platforms with checked pointer
registers would need to store and do all there arithmatic in
non-pointer registers, and then only load these values into the
pointer registers when dereferencing. This is against the
"Don't pay for what you don't use", "Spirit of C" that C++ inherited.


Ahh yes, of course.

I wasn' thinking in terms of platforms that might do that.

Very interesting, thanks.

P.S., sorry for the delay in replying, news server started denying me access for no apparent reason. It's just slow now.

Ben
--
I'm not just a number. To many, I'm known as a String...
Jul 30 '05 #9

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

Similar topics

1
by: mk | last post by:
In C++ one can get an absolute address of data member, having a pointer-to-member and a pointer to class object: struct A { int m; }; void f(A *a) {
4
by: Bryan Parkoff | last post by:
I want to allocate pointer array into memory so pointer array contains ten pointers. It would be 4 bytes per pointer to be total 40 bytes. Looks like below for example. unsigned char* A = new...
10
by: Martin Andert | last post by:
Why ist the following code not working? I want to initialize a string in function, but I am getting those STATUS_ACCESS_VIOLATION exceptions. #include <stdio.h> #include <stdlib.h> void...
6
by: mann! | last post by:
hi can some one please explain how int (*x) declares a pointer to an array and int *x declares an array of pointers?
6
by: Scott | last post by:
Hi All, I can't seem to wrap my head around this one. I have a pointer, int *x; which I can assign:
3
by: jagguy | last post by:
hi could someone tell me a good website that explains pointer to pointers well. Not just simple stuff like 2d arrays passing , or pointing to another int variab.le
2
by: Adam | last post by:
hi how to declare pointer to 3dimention array? for example: I've 2 3D arrays char s1; char s2; and i'd like to have A POINTER, which could point one time to array s1 and second time to array...
4
by: Joonshik Kim | last post by:
I was trying to define 3d array with pointer to pointer. I wrote like following. int ***d; nx = 3; ny = 5; nz = 4; d = (int ***)malloc((int) nx*sizeof(int **)); *d = (int **)malloc((int)...
53
by: subramanian100in | last post by:
I saw this question from www.brainbench.com void *ptr; myStruct myArray; ptr = myArray; Which of the following is the correct way to increment the variable "ptr"? Choice 1 ptr = ptr +...
5
by: Sandeep Patil | last post by:
Hello, I am Sandeep Patil. I have one question about pointer in c and also c++. What is function of pointer? it is address operator. We use pointer to store address of variable to...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.