Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Why pointer to "one past" is allowed but pointer to "one before" is not ?

Reply
Thread Tools

Why pointer to "one past" is allowed but pointer to "one before" is not ?

 
 
spibou@gmail.com
Guest
Posts: n/a
 
      06-23-2006
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

 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=
Guest
Posts: n/a
 
      06-23-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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 ?

Consider code such as

char *str = somestring;
while(*str++) {
...
}

str might end up one place past somestring - nice to allow that.
 
Reply With Quote
 
 
 
 
spibou@gmail.com
Guest
Posts: n/a
 
      06-23-2006

Nils O. Selåsdal wrote:

> (E-Mail Removed) wrote:
> > 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 ?

> Consider code such as
>
> char *str = somestring;
> while(*str++) {
> ...
> }
>
> str might end up one place past somestring - nice to allow that.


Yes it is. My question was why the "opposite" is not allowed too.
One could have just as easily something like
while (source >= beg_of_string) *dest++ = *source-- ;
to copy a string in reverse for example.

Does my example evoke undefined behaviour by the way ?

Spiros Bousbouras

 
Reply With Quote
 
Marc Boyer
Guest
Posts: n/a
 
      06-23-2006
Le 23-06-2006, (E-Mail Removed) <(E-Mail Removed)> a écrit*:
> 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 ?


More useful, yes if you agree than there are more
increasing loop than decreasing ones.

But I believe the real reason is that it is easier to
implement on hardware: you just have to waste 1
memory adress, that is to say, your processor
can adress from 0 up to 2^N-1, then, if all
data are stored bewteen 0 and 2^N-2, then,
'one position past' is at worst 2^N-1, which is
a valid adress for your processor, and pointer
arithmetic still apply.

But, 'one position before' is harder. You can not
have any bound on the size of the reserved memory
at the beginning. Because if an object of size S
is stored at adress N, then, &S+1 is just one char
after the space used to store S, but &S-1 is
'sizeof(S)' char before...

It's a bit hard to explain without any blackboard,
and I am not very good at ASCII art.

Marc Boyer
 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      06-23-2006
(E-Mail Removed) wrote:

> 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 ?


Because a pointer one past any array need only take a single byte (since
only the address of the _first byte_ of the virtual member need be
valid, not any further ones), but a pointer one before the beginning
requires the assignment of memory space the size of an entire array
member. Given that the array member can be a humungous struct containing
arrays of structs of arrays of long doubles, this can cost a lot of
address space that could otherwise be gainfully employed.

Richard
 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      06-23-2006
In article <(E-Mail Removed)4all.nl>,
Richard Bos <(E-Mail Removed)> wrote:

>> 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 ?


>Because a pointer one past any array need only take a single byte (since
>only the address of the _first byte_ of the virtual member need be
>valid, not any further ones), but a pointer one before the beginning
>requires the assignment of memory space the size of an entire array
>member.


That's one reason, but I think a much more compelling one was that
there was lots of existing code that did things like

for(p=proc; p<procNPROC; p++)

and very little that did the reverse.

-- Richard
 
Reply With Quote
 
=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=
Guest
Posts: n/a
 
      06-23-2006
(E-Mail Removed) wrote:
> Nils O. Selåsdal wrote:
>
>> (E-Mail Removed) wrote:
>>> 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 ?

>> Consider code such as
>>
>> char *str = somestring;
>> while(*str++) {
>> ...
>> }
>>
>> str might end up one place past somestring - nice to allow that.

>
> Yes it is. My question was why the "opposite" is not allowed too.


It's much,*much* more common to iterate this way, over the other way,
and probably was when the spec made
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      06-23-2006
(E-Mail Removed) wrote:
> ...
> 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 ?
> ...


There are several different reasons for that. One of them is described
below.

The storage is normally filled with the objects from smaller addresses
to larger addresses (i.e. in the same direction in which array indices
grow). For this reason, it is not unusual to have an object that resides
close to the beginning of the storage. To create a "before" pointer (and
properly support all pointer operations) for such an object might be
either impossible or unjustifiably difficult (since such a pointer would
have to point somewhere before the beginning of the storage). "Beginning
of the storage" in this case does not necessarily stand for the
beginning of physical memory. On a hardware platform with
segmented-memory the beginning of a segment has similar properties.

--
Best regards,
Andrey Tarasevich
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      06-23-2006
(E-Mail Removed) wrote:
> ...
> Yes it is. My question was why the "opposite" is not allowed too.
> One could have just as easily something like
> while (source >= beg_of_string) *dest++ = *source-- ;
> to copy a string in reverse for example.
>
> Does my example evoke undefined behaviour by the way ?
> ...


Formally, it does lead to UB, since it attempts to create a "one before"
pointer.

The problem with your code in its nature is similar to the problem with
the following code

unsigned i;
...
while (i >= 0) dest[i] = source[i--];

Note that an unsigned value will never be negative and the loop will
never end.

Essentially the same thing can happen in case of a pointer. If we think
consider pointers (addresses) as arithmetic values, they are unsigned.
Imagine that your 'beg_of_string' pointer points to address 0. How do
you expect you loop to end in this case? How do you expect to represent
a pointer that is less than '0'?

--
Best regards,
Andrey Tarasevich
 
Reply With Quote
 
William Ahern
Guest
Posts: n/a
 
      06-23-2006
On Fri, 23 Jun 2006 05:40:43 -0700, spibou wrote:

>
> Nils O. Selåsdal wrote:
>
>> (E-Mail Removed) wrote:
>> > 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 ?

>> Consider code such as
>>
>> char *str = somestring;
>> while(*str++) {
>> ...
>> }
>> }
>> str might end up one place past somestring - nice to allow that.

>
> Yes it is. My question was why the "opposite" is not allowed too. One
> could have just as easily something like while (source >= beg_of_string)
> *dest++ = *source-- ; to copy a string in reverse for example.
>
> Does my example evoke undefined behaviour by the way ?
>


Yes. And to see a real world example of such a program failing because of
this (not that undefined means it must fail), try this compiler

http://fabrice.bellard.free.fr/tcc/

w/ your code, using the -b switch (bounds checker). I never heeded the
standard on this point until I started using TCC to improve my code
portability.

I must say there are some circumstances where it is indeed desirable to
iterate backwards. For example, I had to tweak many places in a memory
pool library because I would iterate backwards from a given pointer
reading bookkeeping information until I hit a terminator bit. Took me hours to
figure out why my program would crash using TCC:

/*
* Beginning from *p, work backwards reconstructing the value of an
* rbitsint_t integer. Stop when the highest order bit of *p is set, which
* should have been previously preserved as a marker. Return the
* reconstructed value, setting *end to the last position used of p.
*/
static inline rbitsint_t rbits_get(unsigned char *p, unsigned char **end) {
rbitsint_t i = 0; /* currently typedef to size_t */
int n = 0;

do {
i |= (*p & ~(1 << (CHAR_BIT - 1))) << (n++ * (CHAR_BIT - 1));
} while (!(*(p--) & (1 << (CHAR_BIT - 1))));

*end = p + 1;

return i;
} /* rbits_get() */


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why defining a constant in a method is not allowed but usingself.class.const_set is allowed? Iñaki Baz Castillo Ruby 13 05-01-2011 06:09 PM
why is int a[0] not allowed, but int* a = new int[0] is? haijin.biz@gmail.com C++ 9 04-17-2007 09:01 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Wireless Laptop obtaining IP address but not allowed to get on Int =?Utf-8?B?U3dhbGx5?= Wireless Networking 2 11-11-2006 03:20 PM



Advertisments