473,408 Members | 1,938 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,408 software developers and data experts.

Pointer increment

Hello

I'm wondering about the following thing:

Let's define variable and set a pointer to points to it:

int a = 0;
int * p_a = &a;

Now, according to C99, clause 6.5.6.7 I can treat pointer to "a"
like a pointer to the first element of the array of length 1 for
the purpose of additive operators. So after:

p_a++;

my pointer points to one past last element of the array and
it seems to be ok. But what about the following next statement:

++p_a;

I found the following part of clause 8 a bit unclear:

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

The question is:
the behaviour is undefined when I just incremented pointer more than
one past last
element of the array or it is undefined when I dereference such
incremented pointer.
If the former could somebody give an example of architecture where
just setting
pointer that way produces some sort of exception.

Thanks for your help
Dec 15 '07 #1
2 2266
br****@gmail.com wrote:
Hello

I'm wondering about the following thing:

Let's define variable and set a pointer to points to it:

int a = 0;
int * p_a = &a;

Now, according to C99, clause 6.5.6.7 I can treat pointer to "a"
like a pointer to the first element of the array of length 1 for
the purpose of additive operators. So after:

p_a++;

my pointer points to one past last element of the array and
it seems to be ok. But what about the following next statement:

++p_a;
Identical.
>
I found the following part of clause 8 a bit unclear:

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

The question is:
the behaviour is undefined when I just incremented pointer more than
one past last
element of the array or it is undefined when I dereference such
incremented pointer.
If the former could somebody give an example of architecture where
just setting
pointer that way produces some sort of exception.

Thanks for your help
It is allowed to point to one past an object, but not to actually access
that location. No such guarantees for one before the object.

Dec 15 '07 #2
br****@gmail.com writes:
Let's define variable and set a pointer to points to it:

int a = 0;
int * p_a = &a;

Now, according to C99, clause 6.5.6.7 I can treat pointer to "a"
like a pointer to the first element of the array of length 1 for
the purpose of additive operators. So after:

p_a++;

my pointer points to one past last element of the array and
it seems to be ok. But what about the following next statement:

++p_a;

I found the following part of clause 8 a bit unclear:

"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."
You'd have to say what you find unclear. To me it is clear. The fist
pointer increment is OK, the second is not.
The question is:
the behaviour is undefined when I just incremented pointer more than
one past last
element of the array or it is undefined when I dereference such
incremented pointer.
The pointer arithmetic itself gives the UB. You get UB if you
de-reference the "one past the end" pointer, but just computing the
next pointer is not permitted.
If the former could somebody give an example of architecture where
just setting
pointer that way produces some sort of exception.
A system where every object is in its own bounded memory segment would
be one. I think some ICL machines could do this. Cambridge
University made a machine with hardware security where every object
was in hardware-checked segment and the pointer had access permission
information in it.

Of course, standards are there so your code will also work on machines
not yet designed.

--
Ben.
Dec 15 '07 #3

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

Similar topics

2
by: Asfand Yar Qazi | last post by:
Hello. Partly for learning purposes, I have written a smart pointer class. Could you please tell me what's wrong with it? (I know there's something wrong with it, but just not what!) Note...
22
by: lokman | last post by:
Hi, In the following code, can someone tell me the difference between *p++ and p++ ? I can see both achieve the same result. Thanks a lot !
8
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...
6
by: Bo Sun | last post by:
hi: please take a look at the following code: int p={111, 222, 333, 444}, *q = p, p1, p2, p3; p1 = *q++; p2 = *(q++); p3 = *(++q);
3
by: Denis Palmeiro | last post by:
Hi, I've been reading K&R, and I'm a bit confused. In the book, they have the following function: void writelines(char *lineptr, int nlines ) { while( nlines-- > 0 ) printf("%s\n",...
15
by: ranjeet.gupta | last post by:
Dear All, Which is correct ? Let suppose we allocate the memory for the 100 bytes, int *PtrMemoryBlock = (int *)malloc(100); If I do
2
by: a | last post by:
Hi, func(int* p){ p = p+1; } main(){ int* ptr; //assume ptr is pointing to a valid allocated array func(ptr); //Will ptr equal the original address value //or equal the original address +...
33
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the...
3
by: tfelb | last post by:
Hi group! I have here five different declarations but I have some problems to understand this concept. I know there are more examples if I would use parentheses but I think the following ones...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...

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.