www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Slincing behaviour

reply RenatoL <rexlen gmail.com> writes:
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?
Nov 11 2011
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
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
Nov 11 2011
next sibling parent RenatoL <rexlen gmail.com> writes:
Yes, my fault. As Andrei write on his book

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

Thx.
Nov 11 2011
prev sibling next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
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
Nov 11 2011
prev sibling parent reply "Nick Sabalausky" <a a.a> writes:
"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.
Nov 11 2011
parent reply Simon <s.d.hammett gmail.com> writes:
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
Nov 11 2011
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
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
Nov 11 2011
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
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
Nov 11 2011
parent reply Simon <s.d.hammett gmail.com> writes:
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
Nov 11 2011
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
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
Nov 11 2011
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 11/11/2011 01:42 PM, Steven Schveighoffer wrote:
 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
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
Nov 11 2011
parent reply Steven Schveighoffer <schveiguy yahoo.com > writes:
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 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
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
Nov 11 2011
parent Simon <s.d.hammett gmail.com> writes:
On 11/11/2011 23:23, Steven Schveighoffer wrote:
 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 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
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
Yup, that was what I was on about. Should have been a bit clearer on that. Ta. -- My enormous talent is exceeded only by my outrageous laziness. http://www.ssTk.co.uk
Nov 12 2011