www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - why does this compile fine, but dies when you run it.

reply steven kladitis <stevenkladitis gmail.com> writes:
import std.stdio;

void main(){
int[3] array1 = [ 10, 20, 30 ];
auto array2 = array1; // array2's elements are different
// from array1's
array2[0] = 11;
int[] array3;
//array4[0]=3;
array3[0]=4;
auto array4 = array3;

writeln(array1,'\n',array2,'\n',array3,'\n',array4);
}


-- windows 7 64 bit ( os ) dmd 2.079.0
-- thanks
-- Steven
Mar 21 2018
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Wednesday, 21 March 2018 at 18:53:39 UTC, steven kladitis 
wrote:
 int[] array3;
 array3[0]=4;
array3 is empty. You are trying to set a value that doesn't exist..
Mar 21 2018
parent reply steven kladitis <stevenkladitis gmail.com> writes:
On Wednesday, 21 March 2018 at 18:55:34 UTC, Adam D. Ruppe wrote:
 On Wednesday, 21 March 2018 at 18:53:39 UTC, steven kladitis 
 wrote:
 int[] array3;
 array3[0]=4;
array3 is empty. You are trying to set a value that doesn't exist..
import std.stdio; void main(){ int[3] array1 = [ 10, 20, 30 ]; auto array2 = array1; // array2's elements are different // from array1's array2[0] = 11; int[] array3; //array4[0]=3; array3 ~=4; auto array4 = array3; array4 ~=2; int[string] a1; int[int] a2; a1["V"]=2; a2[4]=3; writeln(array1,'\n',array2,'\n',array3,'\n',array4,'\n',a1,'\n',a2); } -- this works, why?? -- what is the difference between int[] x; and int[int] x; ???? -- thanks -- Steven
Mar 22 2018
parent Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Thursday, 22 March 2018 at 14:05:02 UTC, steven kladitis wrote:
 -- what is the difference between int[] x; and int[int] x; ????
int[] x; is like struct _Int_Array { size_t len; int* ptr; } _Int_Array y; int i = 5; x[i] = i; // this and the line below crash or assert, due to a null pointer *(y.ptr +i) = 5; are both the same; int[int] is a completely different beast. is it a hashtable that maps ints to int. its a lot closer to int[string] than it is to int[] see https://dlang.org/spec/arrays.html vs https://dlang.org/spec/hash-map.html
Mar 22 2018