digitalmars.D.learn - Array operations
- Andy Valencia (24/24) May 02 In the following code, two questions. First, is there any
- monkyyy (28/32) May 02 With basic slices no; I could construct something with overloads
- Nick Treleaven (13/19) May 03 `y` is already a slice `int[]`, so slicing it does not change the
- Andy Valencia (26/35) May 03 At least on 1.40.1 of the ldc2 distro for x86-64, uses the
- Kagamin (2/2) May 03 You link with release version of druntime, try to link with debug
- Kagamin (3/3) May 03 But yeah, core.internal.util.array is wrong design, it should be
- Jonathan M Davis (10/12) May 03 That sounds like an ldc bug then. With dmd, your program gives
- Andy Valencia (6/14) May 04 Ah, you are right:
- Kagamin (2/2) May 05 Actually it's a regression:
In the following code, two questions. First, is there any difference between "x[] = y" and "x[] = y[]"? It appears not. Second, in assigning from arrays of differing sizes, Phobos causes an illegal instruction, rather than the sort of exception I'd have expected. I'm curious why they stepped away from D's exception architecture? Thanks! Andy ```d import std.stdio : writeln; void main() { int[] x = new int[8]; x[] = 1; int[] y = new int[4]; y[] = 2; x[0 .. 4] = y; writeln(x); x[] = 1; x[0 .. 4] = y[]; writeln(x); x[] = y[]; writeln(x); } ```
May 02
On Friday, 2 May 2025 at 22:19:53 UTC, Andy Valencia wrote:In the following code, two questions. First, is there any difference between "x[] = y" and "x[] = y[]"?With basic slices no; I could construct something with overloads tho ```d import std.stdio : writeln; auto foo(ref int[] a, ref int[] b){ a=b; } auto bar(ref int[] a, ref int[] b){ a=b[]; } void foobar(alias F)(){ int[] x = new int[4]; int[] y = new int[8]; F(x,y); (x.ptr-y.ptr).writeln; (x.length-y.length).writeln; } void main() { foobar!foo; foobar!bar; } ``` x and y are as far as I know, bit for bit identical and nothing can cause spookieness in either caseI'm curious why they stepped away from D's exception architecture?Slices are references... mostly; its spooky when they overlap with dynamic arrays; with this instruction your going to be reassigning a pointer.
May 02
On Friday, 2 May 2025 at 22:19:53 UTC, Andy Valencia wrote:In the following code, two questions. First, is there any difference between "x[] = y" and "x[] = y[]"? It appears not.`y` is already a slice `int[]`, so slicing it does not change the type. Slicing without indices selects all elements, it does not change the elements the expression refers to.Second, in assigning from arrays of differing sizes, Phobos causes an illegal instruction, rather than the sort of exception I'd have expected. I'm curious why they stepped away from D's exception architecture?It throws a RangeError rather than an Exception. One advantage is that allows indexing an array to be used in `nothrow` code. Also, bounds checks can be turned off, in which case nothing would be thrown, and code attempting to catch this occurence would never be called. Indexing past the end of a slice is a programming error which is not supposed to be recoverable from. If the slice length mismatch is caused by a runtime value, that's still a programmer error because the runtime value was not checked for validity.
May 03
On Saturday, 3 May 2025 at 11:18:00 UTC, Nick Treleaven wrote:At least on 1.40.1 of the ldc2 distro for x86-64, uses the "illegal instruction" instruction. But it does make sense that array operations want to be outside the "nothrow" guarantee, as that would make nothrow almost useless. Thanks, Andy ``` (gdb) r Starting program: /home/vandys/dlang/tst41 [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1". [2, 2, 2, 2, 1, 1, 1, 1] [2, 2, 2, 2, 1, 1, 1, 1] Program received signal SIGILL, Illegal instruction. 0x00007ffff7f5a9e8 in core.internal.util.array._enforceSameLength(const(char[]), const(ulong), const(ulong)) () from /home/vandys/ldc2-1.39.0-linux-x86_64/bin/../lib/libdruntime-ldc-shared.so.109 (gdb) x/i $pc => 0x7ffff7f5a9e8 <_D4core8internal4util5array18_enforceSameLengthFNbNfxAaxmxmZv+168>: ud2 (gdb) ```Second, in assigning from arrays of differing sizes, Phobos causes an illegal instruction, rather than the sort of exception I'd have expected. I'm curious why they stepped away from D's exception architecture?It throws a RangeError rather than an Exception. One advantage is that allows indexing an array to be used in `nothrow` code. Also, bounds checks can be turned off, in which case nothing would be thrown, and code attempting to catch this occurence would never be called.
May 03
You link with release version of druntime, try to link with debug version.
May 03
But yeah, core.internal.util.array is wrong design, it should be boolean function, and the compiler should generate assert(areTypedArraysConformable()); at the caller side.
May 03
On Saturday, May 3, 2025 8:23:00 AM Mountain Daylight Time Andy Valencia via Digitalmars-d-learn wrote:At least on 1.40.1 of the ldc2 distro for x86-64, uses the "illegal instruction" instruction.That sounds like an ldc bug then. With dmd, your program gives [2, 2, 2, 2, 1, 1, 1, 1] [2, 2, 2, 2, 1, 1, 1, 1] core.exception.RangeError q.d(13): Range violation ---------------- ??:? onRangeError [0x29f6b6] ??:? _d_arrayboundsp [0x29f685] ??:? _Dmain [0x27c099] - Jonathan M Davis
May 03
On Sunday, 4 May 2025 at 00:15:23 UTC, Jonathan M Davis wrote:That sounds like an ldc bug then. With dmd, your program gives [2, 2, 2, 2, 1, 1, 1, 1] [2, 2, 2, 2, 1, 1, 1, 1] core.exception.RangeError q.d(13): Range violation ---------------- ??:? onRangeError [0x29f6b6] ??:? _d_arrayboundsp [0x29f685] ??:? _Dmain [0x27c099]Ah, you are right: https://github.com/ldc-developers/ldc/issues/3793 dmd here give the RangeError also (as does gdc). Thanks, Andy
May 04
Actually it's a regression: https://forum.dlang.org/post/fwwhyqpdvmdyudtgipyg forum.dlang.org
May 05