Comparison of two 'dynamic arrays'.
matheus.
matheus at gmail.com
Sun Nov 13 16:11:17 UTC 2022
On Sunday, 13 November 2022 at 15:45:40 UTC, DLearner wrote:
> On Sunday, 13 November 2022 at 14:39:26 UTC, Siarhei Siamashka
> wrote:
>> On Sunday, 13 November 2022 at 14:28:45 UTC, DLearner wrote:
>>> Creating a step 1.5:
>>> ```
>>> int[] B = A;
>>> ```
>>
>> ```D
>> auto B = A.dup;
>> ```
>>
>> This will create a copy of A rather than referencing to the
>> same buffer in memory.
>
> Tested:
>
> ```
> void main() {
>
> import std.stdio;
>
>
> struct test_struct {
> char[] Txt;
> }
>
> test_struct[] A;
>
>
> A.length = 1;
> A[0].Txt.length = 1;
> A[0].Txt[0] = 'X';
>
> writeln(A);
>
>
> auto B = A.dup;
>
> writeln(A, B);
>
> A[0].Txt[0] = 'Y';
>
> writeln(A, B);
> }
>
> ```
>
> Got:
> ```
> [test_struct("X")]
> [test_struct("X")][test_struct("X")]
> [test_struct("Y")][test_struct("Y")]
> ```
>
> Expected last line to be Y,X not Y,Y.
You should add the code below after "auto B = A.dup;":
B[0].Txt = A[0].Txt.dup;
The "Txt" property inside B is still referencing A without the
above.
Matheus.
More information about the Digitalmars-d-learn
mailing list