www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Strange Runtime Error - Static Arrays

reply Zachary Lund <admin computerquip.com> writes:
Someone brought an example that I thought was rather strange an 
preventable in the IRC this evening. Take this example:

int[3] bob = [ 1, 2, 3];

The above will compile fine and the program may even run fine up until 
the above statement. When the above statement is executed, an 
exceptional error is given:

object.Exception src/rt/arraycat.d(31): lengths don't match for array copy
----------------
./main() [0x8049ec3]
./main() [0x804cbd0]
./main() [0x804c875]
./main() [0x804cc17]
./main() [0x804c875]
./main() [0x804c824]
/lib/libc.so.6(__libc_start_main+0xf3) [0xb764d483]
----------------

Couldn't this be prevented at compile time?
Jan 08 2012
next sibling parent Zachary Lund <admin computerquip.com> writes:
On 01/08/2012 05:33 PM, Zachary Lund wrote:
 Someone brought an example that I thought was rather strange an
 preventable in the IRC this evening. Take this example:

 int[3] bob = [ 1, 2, 3];

 The above will compile fine and the program may even run fine up until
 the above statement. When the above statement is executed, an
 exceptional error is given:

 object.Exception src/rt/arraycat.d(31): lengths don't match for array copy
 ----------------
 ./main() [0x8049ec3]
 ./main() [0x804cbd0]
 ./main() [0x804c875]
 ./main() [0x804cc17]
 ./main() [0x804c875]
 ./main() [0x804c824]
 /lib/libc.so.6(__libc_start_main+0xf3) [0xb764d483]
 ----------------

 Couldn't this be prevented at compile time?

Er, wrong line given... int[4] bob = [ 1, 2, 3 ];
Jan 08 2012
prev sibling next sibling parent Mail Mantis <mail.mantis.88 gmail.com> writes:
2012/1/9 Zachary Lund <admin computerquip.com>:
 On 01/08/2012 05:33 PM, Zachary Lund wrote:
 Someone brought an example that I thought was rather strange an
 preventable in the IRC this evening. Take this example:

 int[3] bob = [ 1, 2, 3];

 The above will compile fine and the program may even run fine up until
 the above statement. When the above statement is executed, an
 exceptional error is given:

 object.Exception src/rt/arraycat.d(31): lengths don't match for array copy
 ----------------
 ./main() [0x8049ec3]
 ./main() [0x804cbd0]
 ./main() [0x804c875]
 ./main() [0x804cc17]
 ./main() [0x804c875]
 ./main() [0x804c824]
 /lib/libc.so.6(__libc_start_main+0xf3) [0xb764d483]
 ----------------

 Couldn't this be prevented at compile time?

Er, wrong line given... int[4] bob = [ 1, 2, 3 ];

Try this example: int[4] bob; writeln( typeof(bob).stringof ); // int[4u] writeln( typeof([1, 2, 3]).stringof ); // int[] It seems that array literal evaluates to dynamic array. Why it works so, I don't know, but seemingly it is the cause of that odd behaviour.
Jan 08 2012
prev sibling next sibling parent Trass3r <un known.com> writes:
 Couldn't this be prevented at compile time?

https://github.com/D-Programming-Language/dmd/pull/375
Jan 08 2012
prev sibling parent bearophile <bearophileHUGS lycos.com> writes:
Zachary Lund:

 Couldn't this be prevented at compile time?

It should not give this error at run time. Such run-time error should be avoided with all the energy possible. Isn't this somewhere in Bugzilla already? Bye, bearophile
Jan 08 2012