Slincing behaviour

Steven Schveighoffer schveiguy at yahoo.com
Fri Nov 11 15:23:45 PST 2011


Ali Çehreli Wrote:

> On 11/11/2011 01:42 PM, Steven Schveighoffer wrote:
> > On Fri, 11 Nov 2011 16:10:12 -0500, Simon <s.d.hammett at gmail.com> wrote:
> >
> >> On 11/11/2011 19:04, Steven Schveighoffer wrote:
> >>> On Fri, 11 Nov 2011 14:01:42 -0500, Steven Schveighoffer
> >>> <schveiguy at yahoo.com> wrote:
> >>>
> >>>> There should be no bounds error in any case, an empty slice is valid.
> >>>
> >>> By "in any case" I meant in either debug or release mode.
> >>>
> >>> -Steve
> >>
> >> even when you index beyond the bounds of the slice?
> >>
> >> you may not actually be reading memory because it's zero length, but
> >> it's still logically invalid; you've gone outside the valid range.
> >
> > You are not reading beyond the valid range. A zero-length slice is
> > perfectly legal to point at the end of an array or other slice. Reading
> > any data from a zero-length slice will cause an out-of-bounds error in
> > debug mode, because it has no elements.
> >
> >> in vc9, if you increment an iterator beyond the valid range you get a
> >> debug assert. that's caught quite a few bugs where I work when we
> >> upgraded to vc9.
> >
> > I think you are misunderstanding what the $ actually means.
> >
> > It's the equivalent in C++ iterators to x.end.
> >
> > The pair of iterators x.end, x.end is a valid range. Going *beyond*
> > x.end would be illegal. But iterating *to* x.end is legal (which would
> > be the equivalent of [$..$] range), and you will not be able to convince
> > me that vc9 doesn't allow it.
> >
> > -Steve
> 
> How about Jonathan's this comment: "It wouldn't surprise me if arr[500 
> .. 500] worked exactly the same way. Because the array is empty, it 
> doesn't really matter  what values you gave it."
> 
> I think Simon is objecting to 500..500 being accepted (if at all). I 
> agree that $..$ is correct.
> 
> Ali

Oh my mistake!  I thought we were talking about arr[$..$]!

Yes, arr[500..500] would result in bounds errors if the array is only 7 elements. When I read it I thought it was just an arbitrary example to show another way to get an empty slice with an assumption that 500 is a valid index. 

-Steve


More information about the Digitalmars-d-learn mailing list