Topic: Pointers Beyond Array Boundries


Author: clamage@Eng.sun.com (Steve Clamage)
Date: 1996/07/23
Raw View
In article EEI@ennews.eas.asu.edu, dancho@enuxsa.eas.asu.edu (Mark Dancho ) writes:
>
>Section 5.7 of the ARM states, "If the resulting pointer points outside
>the bounds of the array, except at the first location beyond the high
>end of the array, the result is undefined."  Section 5.9 gives an
>example of why they allow pointers to point one element beyond the high
>end of the array:
>
>   int a[100];
>   for (int* p = a; p<&a[100]; p++) { /* ... */ }

It is the same rule as in C, and for the same reason.

>However, I don't see any guarantee that allows arrays to be iterated
>from the high element in the array through the low element:
>
>   int a[100];
>   for (int* p = &a[100]; p > a; p--) { /* ... */ }

Your example never causes p to point before a[0], and so does not cause
a problem. (Assuming in the loop you reference p[-1] instead of *p.)

Perhaps you meant to create an example where you compared p to &a[-1],
or decremented p after it pointed to a[0].

That isn't guaranteed to work because it would potentially require
implementations to insert padding of sizeof(a[0]) in front of the array.
In the general case, &a[-1] might not be a valid expression otherwise.
(Suppose addresses can't be negative and 'a' is allocated at an
address between 0 and sizeof(a[0]). Or suppose 'a' is on the heap, and
the system uses segment+offset addresses such that every heap object
is at offset 0.)

The requirement that &a[100] be valid requires only that you can express
the address of the byte after the end of the array, a much less onerous
requirement.

---
Steve Clamage, stephen.clamage@eng.sun.com




[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1996/07/24
Raw View
dancho@enuxsa.eas.asu.edu (Mark Dancho ) writes:

>However, I don't see any guarantee that allows arrays to be iterated
>from the high element in the array through the low element:
>
>   int a[100];
>   for (int* p = &a[100]; p > a; p--) { /* ... */ }

Perhaps you meant

    for (int* p = &a[99]; p >= a; p--) { /* ... */ }

?  That would have undefined behaviour, because at the end of the loop
p would be a-1.

I guess one reason that there's no reason to support such code is
that it's still possible to iterate backwards through an array,
using code like this:

    for (int* p = &a[100]; p > a; ) { p--; /* ... */ }

--
Fergus Henderson <fjh@cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3         |     -- the last words of T. S. Garp.
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: maxwell@natasha.jpl.nasa.gov (Scott Maxwell)
Date: 1996/07/25
Raw View
> However, I don't see any guarantee that allows arrays to be iterated
> from the high element in the array through the low element:
>
>    int a[100];
>    for (int* p = &a[100]; p > a; p--) { /* ... */ }

(What you typed wasn't what I think you meant, but ...)

Why not do this:

    for (int * p = &a[100]; p > a; )  {  --p;  /* ... */  }

I'll sure feel like a fool if I've overlooked something, but this
looks correct and (draft-)standard-compliant to me.

--
-------------------------+----------------------------------------------------
   //  Scott Maxwell:    | ``Probably the earliest fly swatters were nothing
\\//      maxwell@       | more than some sort of striking surface attached to
 XX natasha.jpl.nasa.gov | the end of a long stick.''  -- _Deep Thoughts_, SNL
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: 100653.2230@compuserve.com (Raoul De Kezel)
Date: 1996/07/29
Raw View
fjh@mundook.cs.mu.OZ.AU (Fergus Henderson) wrote:

>dancho@enuxsa.eas.asu.edu (Mark Dancho ) writes:

>>However, I don't see any guarantee that allows arrays to be iterated
>>from the high element in the array through the low element:
>>
>>   int a[100];
>>   for (int* p = &a[100]; p > a; p--) { /* ... */ }

>Perhaps you meant

>    for (int* p = &a[99]; p >= a; p--) { /* ... */ }

This reminds me that a friend suggested recently to write

    for (int* p = &a[100]; p  --> a;  ) { /* ... */ }

--- Raoul
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: dancho@enuxsa.eas.asu.edu (Mark Dancho )
Date: 1996/07/23
Raw View

Section 5.7 of the ARM states, "If the resulting pointer points outside
the bounds of the array, except at the first location beyond the high
end of the array, the result is undefined."  Section 5.9 gives an
example of why they allow pointers to point one element beyond the high
end of the array:

   int a[100];
   for (int* p = a; p<&a[100]; p++) { /* ... */ }

However, I don't see any guarantee that allows arrays to be iterated
from the high element in the array through the low element:

   int a[100];
   for (int* p = &a[100]; p > a; p--) { /* ... */ }

I've seen code both in books and in practice that relies on a pointer
comparison one element beyond the low end of the array.  Why doesn't C++
guarentee that such a comparison is valid?

--
Mark Dancho      "You may be disappointed if you fail,
Dancho@asu.edu    but you are doomed if you don't try." --Beverly Sills



[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]