digitalmars.D.learn - Slincing behaviour
- RenatoL (4/4) Nov 11 2011 int[7] arr = [1,2,3,4,5,6,7];
- Jonathan M Davis (6/11) Nov 11 2011 Probably because it's length is zero and therefore isn't actually a slic...
- RenatoL (3/3) Nov 11 2011 Yes, my fault. As Andrei write on his book
- Steven Schveighoffer (8/21) Nov 11 2011 I would just make a minor correction that it *is* a slice of the origina...
- Nick Sabalausky (4/16) Nov 11 2011 Weird, I was certain that had given me bounds errors before, and so I've...
- Simon (6/24) Nov 11 2011 You get bounds error in debug, but not release.
- Steven Schveighoffer (3/30) Nov 11 2011 There should be no bounds error in any case, an empty slice is valid.
- Steven Schveighoffer (4/5) Nov 11 2011 By "in any case" I meant in either debug or release mode.
- Simon (10/15) Nov 11 2011 even when you index beyond the bounds of the slice?
- Steven Schveighoffer (12/27) Nov 11 2011 You are not reading beyond the valid range. A zero-length slice is
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (7/36) Nov 11 2011 How about Jonathan's this comment: "It wouldn't surprise me if arr[500
- Steven Schveighoffer (4/50) Nov 11 2011 Oh my mistake! I thought we were talking about arr[$..$]!
- Simon (6/56) Nov 12 2011 Yup, that was what I was on about. Should have been a bit clearer on
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
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
Yes, my fault. As Andrei write on his book The situation m == n is acceptable and yields an empty slice Thx.
Nov 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: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). -Steveint[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.
Nov 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: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.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.
Nov 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...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.ukOn Friday, November 11, 2011 11:46:02 RenatoL wrote: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.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.
Nov 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:There should be no bounds error in any case, an empty slice is valid. -Steve"Jonathan M Davis"<jmdavisProg gmx.com> wrote in message news:mailman.866.1321013026.24802.digitalmars-d-learn puremagic.com...You get bounds error in debug, but not release. The bit about slices says you must not depend on bounds checking.On Friday, November 11, 2011 11:46:02 RenatoL wrote: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.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.
Nov 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
Nov 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: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.ukThere 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
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: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.On Fri, 11 Nov 2011 14:01:42 -0500, Steven Schveighoffer <schveiguy yahoo.com> wrote: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.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. -Stevein 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
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: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. AliOn 11/11/2011 19:04, Steven Schveighoffer wrote: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.On Fri, 11 Nov 2011 14:01:42 -0500, Steven Schveighoffer <schveiguy yahoo.com> wrote: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.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. -Stevein 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
Ali Çehreli Wrote:On 11/11/2011 01:42 PM, Steven Schveighoffer wrote: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. -SteveOn Fri, 11 Nov 2011 16:10:12 -0500, Simon <s.d.hammett gmail.com> wrote: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. AliOn 11/11/2011 19:04, Steven Schveighoffer wrote: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.On Fri, 11 Nov 2011 14:01:42 -0500, Steven Schveighoffer <schveiguy yahoo.com> wrote: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.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. -Stevein 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
On 11/11/2011 23:23, Steven Schveighoffer wrote:Ali Çehreli Wrote: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.ukOn 11/11/2011 01:42 PM, Steven Schveighoffer wrote: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. -SteveOn Fri, 11 Nov 2011 16:10:12 -0500, Simon<s.d.hammett gmail.com> wrote: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. AliOn 11/11/2011 19:04, Steven Schveighoffer wrote: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.On Fri, 11 Nov 2011 14:01:42 -0500, Steven Schveighoffer <schveiguy yahoo.com> wrote: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.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. -Stevein 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 12 2011