Code security: "auto" / Reason for errors

Ozan via Digitalmars-d digitalmars-d at puremagic.com
Wed Mar 2 11:42:02 PST 2016


Hi

I despair of "auto var1 = var2"for arrays. Isn't it a open door 
for errors. Example

import std.stdio;

void main()
{
	int[] a;
	foreach(i; 0..10) a ~= i;
	auto b = a; // correct dlang coding: auto b = a.dup;
	
	a[2] = 1;
	b[2] = 5; // Overwrites assignment before
	writeln(a);
	writeln(b); // Always a == b but developer would like to have (a 
!= b)
}

The behaviour is different to other non-container datatypes.
So in a first view, it looks like a data copy but it's only a 
pointer copy.

Regards, Ozan


More information about the Digitalmars-d mailing list