how do you append arrays?
Nicholas Wilson via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Feb 25 05:33:46 PST 2016
On Thursday, 25 February 2016 at 13:24:09 UTC, asdf wrote:
> On Thursday, 25 February 2016 at 13:06:10 UTC, cym13 wrote:
>>>
>>> In D the binary operator "~" is used to concatenate both
>>> strings (arrays of characters) and arrays. (also the ~=
>>> operator is equivalent to lhs = lhs ~ rhs
>>>
>>> Nic
>>
>> Just a precision: "lhs ~= rhs" isn't exactly equivalent to
>> "lhs = lhs ~ rhs", those are two distinct operators that may
>> deal with memory etc in different ways. For arrays doing "lhs
>> = lhs ~ rhs" will first create (and allocate) the array
>> corresponding to "lhs ~ rhs" and then assign this new array
>> to lhs. On the other hand "lhs ~= rhs" realises in-place
>> append.
>
> I tried both, the error this time is:
> object.Exception@/data/data/com.termux/files/home/ldc/runtime/druntime/src/ldc/arrayinit.d(151): overlapping array copy
so you will have to make a copy and then move it
Im assuming
history[1..100] = history[0..99];
this is the line causing your problem.
Note that D has zero based array indexing
so assuming your array has 100 elements history[1..100]
is going one past the end of the array.
also using a circular buffer here will solve this problem.
Nic
More information about the Digitalmars-d-learn
mailing list