Jump to page: 1 2
Thread overview
Slincing behaviour
Nov 11, 2011
RenatoL
Nov 11, 2011
Jonathan M Davis
Nov 11, 2011
RenatoL
Nov 11, 2011
Nick Sabalausky
Nov 11, 2011
Simon
Nov 11, 2011
Simon
Nov 11, 2011
Ali Çehreli
Nov 12, 2011
Simon
November 11, 2011
int[7] arr = [1,2,3,4,5,6,7];
writeln(arr[$..$]);

this simply prints a newline... I expected a runtime error (or better a compile time error) but it does nothing ... why?
November 11, 2011
On Friday, November 11, 2011 11:46:02 RenatoL wrote:
> int[7] arr = [1,2,3,4,5,6,7];
> writeln(arr[$..$]);
> 
> this simply prints a newline... I expected a runtime error (or better a compile time error) but it does nothing ... why?

Probably because it's length is zero and therefore isn't actually a slice of the original array at all. 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.

- Jonathan M Davis
November 11, 2011
Yes, my fault. As Andrei write on his book

The situation m == n is acceptable and yields an empty slice

Thx.
November 11, 2011
On Fri, 11 Nov 2011 07:03:33 -0500, Jonathan M Davis <jmdavisProg@gmx.com> wrote:

> On Friday, November 11, 2011 11:46:02 RenatoL wrote:
>> int[7] arr = [1,2,3,4,5,6,7];
>> writeln(arr[$..$]);
>>
>> this simply prints a newline... I expected a runtime error (or better
>> a compile time error) but it does nothing ... why?
>
> Probably because it's length is zero and therefore isn't actually a slice of
> the original array at all. 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 would just make a minor correction that it *is* a slice of the original array.  It's just an empty slice.

BTW, I would have expected the output:

[]

(and testing, I see it does do this).

-Steve
November 11, 2011
"Jonathan M Davis" <jmdavisProg@gmx.com> wrote in message news:mailman.866.1321013026.24802.digitalmars-d-learn@puremagic.com...
> On Friday, November 11, 2011 11:46:02 RenatoL wrote:
>> int[7] arr = [1,2,3,4,5,6,7];
>> writeln(arr[$..$]);
>>
>> this simply prints a newline... I expected a runtime error (or better a compile time error) but it does nothing ... why?
>
> Probably because it's length is zero and therefore isn't actually a slice
> of
> the original array at all. 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.
>

Weird, I was certain that had given me bounds errors before, and so I've been careful to avoid doing it. Maybe that's changed since I last tried it.


November 11, 2011
On 11/11/2011 16:56, Nick Sabalausky wrote:
> "Jonathan M Davis"<jmdavisProg@gmx.com>  wrote in message
> news:mailman.866.1321013026.24802.digitalmars-d-learn@puremagic.com...
>> On Friday, November 11, 2011 11:46:02 RenatoL wrote:
>>> int[7] arr = [1,2,3,4,5,6,7];
>>> writeln(arr[$..$]);
>>>
>>> this simply prints a newline... I expected a runtime error (or better
>>> a compile time error) but it does nothing ... why?
>>
>> Probably because it's length is zero and therefore isn't actually a slice
>> of
>> the original array at all. 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.
>>
>
> Weird, I was certain that had given me bounds errors before, and so I've
> been careful to avoid doing it. Maybe that's changed since I last tried it.
>
>

You get bounds error in debug, but not release.
The bit about slices says you must not depend on bounds checking.

-- 
My enormous talent is exceeded only by my outrageous laziness.
http://www.ssTk.co.uk
November 11, 2011
On Fri, 11 Nov 2011 13:29:17 -0500, Simon <s.d.hammett@gmail.com> wrote:

> On 11/11/2011 16:56, Nick Sabalausky wrote:
>> "Jonathan M Davis"<jmdavisProg@gmx.com>  wrote in message
>> news:mailman.866.1321013026.24802.digitalmars-d-learn@puremagic.com...
>>> On Friday, November 11, 2011 11:46:02 RenatoL wrote:
>>>> int[7] arr = [1,2,3,4,5,6,7];
>>>> writeln(arr[$..$]);
>>>>
>>>> this simply prints a newline... I expected a runtime error (or better
>>>> a compile time error) but it does nothing ... why?
>>>
>>> Probably because it's length is zero and therefore isn't actually a slice
>>> of
>>> the original array at all. 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.
>>>
>>
>> Weird, I was certain that had given me bounds errors before, and so I've
>> been careful to avoid doing it. Maybe that's changed since I last tried it.
>>
>>
>
> You get bounds error in debug, but not release.
> The bit about slices says you must not depend on bounds checking.
>

There should be no bounds error in any case, an empty slice is valid.

-Steve
November 11, 2011
On Fri, 11 Nov 2011 14:01:42 -0500, Steven Schveighoffer <schveiguy@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
November 11, 2011
On 11/11/2011 19:04, Steven Schveighoffer wrote:
> On Fri, 11 Nov 2011 14:01:42 -0500, Steven Schveighoffer
> <schveiguy@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.

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.

-- 
My enormous talent is exceeded only by my outrageous laziness.
http://www.ssTk.co.uk
November 11, 2011
On Fri, 11 Nov 2011 16:10:12 -0500, Simon <s.d.hammett@gmail.com> wrote:

> On 11/11/2011 19:04, Steven Schveighoffer wrote:
>> On Fri, 11 Nov 2011 14:01:42 -0500, Steven Schveighoffer
>> <schveiguy@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
« First   ‹ Prev
1 2