Code security: "auto" / Reason for errors

Daniel Kozak via Digitalmars-d digitalmars-d at puremagic.com
Wed Mar 2 23:03:59 PST 2016


Dne 3.3.2016 v 03:39 Jack Stouffer via Digitalmars-d napsal(a):
>  Dynamic arrays are reference types in D;
No, they are value types, but mimic reference types in some cases

void fun(int[] arr) {
     arr ~= 10;
}

void fun2(ref int[] arr) {
     arr ~= 10;
}

void mod(int[] arr) {
     if (arr.length) arr[0] = 7;
}

void mod2(ref int[] arr) {
     if (arr.length) arr[0] = 7;
}

void main() {
     int[] arr;
     int[] arr2 = [1];

     fun(arr);
     fun(arr2);
     writeln(arr); // []
     writeln(arr2); // [1]

     fun2(arr);
     fun2(arr2);
     writeln(arr); // [10]
     writeln(arr2); // [1, 10]

     mod(arr);
     mod(arr2);
     writeln(arr); // [7]
     writeln(arr2); // [7, 10]

     mod(arr);
     mod(arr2);
     writeln(arr); // [7]
     writeln(arr2); // [7, 10]
}




More information about the Digitalmars-d mailing list