Code security: "auto" / Reason for errors

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Wed Mar 2 13:37:56 PST 2016


On 3/2/16 3:01 PM, Ozan wrote:
>> It'd do exactly the same thing if you wrote
>>
>> int[] b = a;
>>
>> so the auto changes nothing there. Generally, the slice assignment is
>> a good thing because it gives you easy efficiency and doesn't hide the
>> costs of a duplicate.
>
> I agree for slices, but typically variables should have his own data.
> To add additional code for separate data has a lot of risk in my mind.
> Behaviors should also be the same.
>
> int a = 1:
> int b = a;  // data copy
>
> int[] a;
> int[] b = a; // pointer copy
>
> is not the same and should be avoid.

Pointer copying is inherent in D. Everything is done at the "head", deep 
copies are never implicit. This is a C-like language, so one must expect 
this kind of behavior and plan for it.

If you want a self-duplicating dynamic array, you can write one pretty 
easily with postblit. If you use static arrays, then the copying 
behavior is what you get (because there is no pointer).

What is the danger you are concerned about?

-Steve


More information about the Digitalmars-d mailing list