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

Legality of pointer arithmetic

I've been looking at the committee draft of the C99 specification,
specifically the one at this URI:
http://www.open-std.org/jtc1/sc22/wg.../docs/n843.pdf. I don't have
a copy of the official international standard, but I assume that it is
similar.

The rules for pointer arithmetic (6.5.6.7-8) are appended below for
ease of reference. They seem astonishingly restrictive. As far as I can
see, there is no guarantee that the code:

char *p = malloc(10); /* assume this malloc succeeds */
p[2] = 'x';

will write an 'x' into the third byte of the allocated store; in fact,
the behaviour of that code is completely undefined.

(The expression p[2] is defined to be equivalent to *(p+2); p+2 is not
part of the same array object as p, hence the behaviour is undefined.)

Can that possibly be right? Am I misreading the spec, is the spec
misrepresenting the language (or is there another possiblity)?

In anticipation of your collective wisdom,
Robin

From section 6.5.6 "Additive operators":

7 For the purposes of these operators, a pointer to a nonarray object
behaves the same as a pointer to the first element of an array of
length one with the type of the object as its element type.

8 When an expression that has integer type is added to or subtracted
from a pointer, the result has the type of the pointer operand. If the
pointer operand points to an element of an array object, and the array
is large enough, the result points to an element offset from the
original element such that the difference of the subscripts of the
resulting and original array elements equals the integer expression. In
other words, if the expression P points to the i-th element of an array
object, the expressions (P)+N (equivalently, N+(P)) and (P)-N (where N
has the value n) point to, respectively, the i+n-th and i-n-th
elements of the array object, provided they exist. Moreover, if the
expression P points to the last element of an array object, the
expression (P)+1 points one past the last element of the array object,
and if the expression Q points one past the last element of an array
object, the expression (Q)-1 points to the last element of the array
object. 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. If the result points one past the last element
of the array object, it shall not be used as the operand of a unary *
operator that is evaluated.

Nov 15 '05 #1
10 1604
ho******@mailinator.com wrote:
I've been looking at the committee draft of the C99 specification,
specifically the one at this URI:
http://www.open-std.org/jtc1/sc22/wg.../docs/n843.pdf. I don't have
a copy of the official international standard, but I assume that it is
similar.
The most recent draft is n869 available at:
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n869/

The rules for pointer arithmetic (6.5.6.7-8) are appended below for
ease of reference. They seem astonishingly restrictive. As far as I can
see, there is no guarantee that the code:

char *p = malloc(10); /* assume this malloc succeeds */
p[2] = 'x';

will write an 'x' into the third byte of the allocated store; in fact,
the behaviour of that code is completely undefined.

(The expression p[2] is defined to be equivalent to *(p+2); p+2 is not
part of the same array object as p, hence the behaviour is undefined.)


7.20.3p1 (malloc):
'The pointer returned if the allocation succeeds is suitably aligned so
that it may be assigned to a pointer to any type of object and then
used to access such an object or an array of such objects in the space
allocated'

Robert Gamble

Nov 15 '05 #2
Robert Gamble wrote:

ho******@mailinator.com wrote:
I've been looking at the committee draft of the C99 specification,
specifically the one at this URI:
http://www.open-std.org/jtc1/sc22/wg.../docs/n843.pdf. I don't have
a copy of the official international standard, but I assume that it is
similar.


The most recent draft is n869 available at:
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n869/


try: <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf>

which is the 2005 draft incorporating the corrigendae.
Unfortunately there is no text version.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson

Nov 15 '05 #3


ho******@mailinator.com wrote:
I've been looking at the committee draft of the C99 specification,
specifically the one at this URI:
http://www.open-std.org/jtc1/sc22/wg.../docs/n843.pdf. I don't have
a copy of the official international standard, but I assume that it is
similar.

The rules for pointer arithmetic (6.5.6.7-8) are appended below for
ease of reference. They seem astonishingly restrictive. As far as I can
see, there is no guarantee that the code:

char *p = malloc(10); /* assume this malloc succeeds */
p[2] = 'x';

will write an 'x' into the third byte of the allocated store; in fact,
the behaviour of that code is completely undefined.

(The expression p[2] is defined to be equivalent to *(p+2); p+2 is not
part of the same array object as p, hence the behaviour is undefined.)
[...]


You've overlooked 7.20.3: "[...] The pointer returned [...]
may be assigned to a pointer to any type of object and then used
to access such an object *or an array of such objects* [...]"
(Emphasis mine.)

--
Er*********@sun.com

Nov 15 '05 #4
Robert Gamble wrote:
The most recent draft is n869 available at:
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n869/
That's useful to know. Thanks. (Also thanks to CBFalconer for the link
to the 2005 draft.)
7.20.3p1 (malloc):
'The pointer returned if the allocation succeeds is suitably aligned so
that it may be assigned to a pointer to any type of object and then
used to access such an object or an array of such objects in the space
allocated'


Ah yes, I overlooked that sentence. Thanks again, and to Eric Sosman
for pointing out the same thing.

Robin

Nov 15 '05 #5
CBFalconer <cb********@yahoo.com> writes:
Robert Gamble wrote:

ho******@mailinator.com wrote:
I've been looking at the committee draft of the C99 specification,
specifically the one at this URI:
http://www.open-std.org/jtc1/sc22/wg.../docs/n843.pdf. I don't have
a copy of the official international standard, but I assume that it is
similar.


The most recent draft is n869 available at:
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n869/


try: <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf>

which is the 2005 draft incorporating the corrigendae.
Unfortunately there is no text version.


I believe the plural of corrigendum is corrigenda,
not corrigendae.
Nov 15 '05 #6
Tim Rentsch wrote:
CBFalconer <cb********@yahoo.com> writes:
Robert Gamble wrote:
ho******@mailinator.com wrote:

I've been looking at the committee draft of the C99 specification,
specifically the one at this URI:
http://www.open-std.org/jtc1/sc22/wg.../docs/n843.pdf. I don't
have a copy of the official international standard, but I assume
that it is similar.

The most recent draft is n869 available at:
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n869/


try: <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf>

which is the 2005 draft incorporating the corrigendae.
Unfortunately there is no text version.


I believe the plural of corrigendum is corrigenda, not corrigendae.


By getting abysmal marks in latin in grade 10, I managed to
persuade my parents to let me replace it with mathematics. The
effects still linger.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 15 '05 #7
CBFalconer <cb********@yahoo.com> writes:
Tim Rentsch wrote:
CBFalconer <cb********@yahoo.com> writes:
Robert Gamble wrote:
ho******@mailinator.com wrote:

> I've been looking at the committee draft of the C99 specification,
> specifically the one at this URI:
> http://www.open-std.org/jtc1/sc22/wg.../docs/n843.pdf. I don't
> have a copy of the official international standard, but I assume
> that it is similar.

The most recent draft is n869 available at:
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n869/

try: <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf>

which is the 2005 draft incorporating the corrigendae.
Unfortunately there is no text version.


I believe the plural of corrigendum is corrigenda, not corrigendae.


By getting abysmal marks in latin in grade 10, I managed to
persuade my parents to let me replace it with mathematics. The
effects still linger.


Lucky you. I had to get poor marks in latin for two years
before I could stop taking it.

In this case, it wasn't my HS latin that led me to look up
corrigendum/corrigenda, but a parallel construction with
agendum/agenda. This small but interesting item is one of
many that came from reading "Quiddities", by W. V. Quine. I
thoroughly recommend it.

"I have been accused of denying consciousness, but I'm not
conscious of having done so." - W. V. Quine, in "Quiddities"
Nov 15 '05 #8
Tim Rentsch <tx*@alumnus.caltech.edu> wrote:

I believe the plural of corrigendum is corrigenda,
not corrigendae.


In English, the plural is corrigendums. :-)

-Larry Jones

Oh, now don't YOU start on me. -- Calvin
Nov 15 '05 #9
la************@ugs.com wrote:
Tim Rentsch <tx*@alumnus.caltech.edu> wrote:

I believe the plural of corrigendum is corrigenda,
not corrigendae.


In English, the plural is corrigendums. :-)


Which version of English are you referring to?

http://dictionary.reference.com/search?q=corrigendum

Nov 15 '05 #10


Old Wolf wrote:
la************@ugs.com wrote:
Tim Rentsch <tx*@alumnus.caltech.edu> wrote:
I believe the plural of corrigendum is corrigenda,
not corrigendae.


In English, the plural is corrigendums. :-)

Which version of English are you referring to?


The corrigendified version, obviouslywise.

--
Er*********@sun.com

Nov 15 '05 #11

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

Similar topics

8
by: ceo | last post by:
Hi, Following is a program that doesn't give the expected output, not sure what's wrong here. I'm adding the size of derived class to the base class pointer to access the next element in the...
22
by: Alex Fraser | last post by:
From searching Google Groups, I understand that void pointer arithmetic is a constraint violation, which is understandable. However, generic functions like qsort() and bsearch() must in essence do...
6
by: Francois Grieu | last post by:
Are these programs correct ? #include <stdio.h> unsigned char a = {1,2}; int main(void) { unsigned char j; for(j=1; j<=2; ++j) printf("%u\n", *( a+j-1 )); return 0; }
3
by: randomtalk | last post by:
hello everyone! Well, recently i've been trying to pick up c and see what is pointer all about (been programming in lisp/python for the better part of my last two years).. mmm.. I'm currently...
27
by: Erik de Castro Lopo | last post by:
Hi all, The GNU C compiler allows a void pointer to be incremented and the behaviour is equivalent to incrementing a char pointer. Is this legal C99 or is this a GNU C extention? Thanks in...
7
by: barikat | last post by:
int a; int *Ptr1, *Ptr2; Ptr1 = a; Ptr1++; Ptr2 = a; printf("Ptr1 : %p\n", Ptr1); printf("Ptr2 : %p\n\n", Ptr2);
26
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I...
19
by: =?iso-8859-1?b?VG9t4XMg0yBoyWlsaWRoZQ==?= | last post by:
Coming originally from C++, I used to do the likes of the following, using a pointer in a conditional: void Func(int *p) { if (p) { *p++ = 7; *p++ = 8;
25
by: Ioannis Vranos | last post by:
Are the following codes guaranteed to work always? 1. #include <iostream> inline void some_func(int *p, const std::size_t SIZE) {
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.