cloning array
Basile.B
b2.temp at gmx.com
Wed Jun 2 15:59:38 UTC 2021
On Wednesday, 2 June 2021 at 15:32:38 UTC, Sean wrote:
> Hello
>
> I have seen this :
> https://forum.dlang.org/thread/1473526717.1917.20.camel@winder.org.uk
>
> Now, please consider this code:
>
>
> import std.stdio;
> import std.math;
> import std.stdio;
> import std.conv;
> import std.format;
> import std.math;
> import std.algorithm;
> import std.net.curl;
> import std.json;
> import std.path;
> import std.array;
> import std.net.curl;
> import core.stdc.stdlib;
> import std.datetime;
> import std.file;
>
>
>
>
>
> void main() {
>
> auto a = new double[][] (0,0);
> a ~= [ 1.0, 2.45];
> a ~= [ 4.9 ,28, 9];
>
>
>
> auto b = a.dup;
>
> a[1] = remove(a[1],1);
>
> writeln(b);
> }
>
>
>
> I compile with dmd version :
>
> DMD64 D Compiler v2.096.1
>
> Copyright (C) 1999-2021 by The D Language Foundation, All
> Rights Reserved written by Walter Bright
>
>
> Output :
>
>
> [[1, 2.45], [4.9, 9, 9]]
>
>
> What I would expect :
>
> [[1, 2.45], [4.9, 28, 9]], because i am copying the array `a`
> to a different place - at `b`.
>
>
> Is this normal behavior of dup? if so, how can I get the
> behavior i am searching for? Thank you.
it's rather an unintuitive behavior.
`.dup` is not a deep copy:
---
import std;
void main() {
auto a = new double[][] (0,0);
a ~= [ 1.0, 2.45];
a ~= [ 4.9 ,28, 9];
auto b = [a[0].dup, a[1].dup];
a[1] = remove(a[1],1);
writeln(b);
}
---
works as you expect
More information about the Digitalmars-d-learn
mailing list