bug in assigning to dynamic array element

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Mon Nov 3 06:24:15 PST 2014


On 11/1/14 8:04 AM, ketmar via Digitalmars-d wrote:
> On Sat, 01 Nov 2014 11:55:53 +0000
> anonymous via Digitalmars-d <digitalmars-d at puremagic.com> wrote:
>
>> On Saturday, 1 November 2014 at 11:50:34 UTC, ketmar via
>> Digitalmars-d wrote:
>>> this *IS* a bug. either compiler should error on this, or it
>>> shouldn't
>>> modify random memory. imagine the situation when old array
>>> contents not
>>> only collected by GC, but that memory was allocated to something
>>> completely different.
>>
>> The old array is still alive and kicking. The left-hand side
>> still references it. It wasn't collected. You're not writing to
>> random memory.
> so it's not only writes to the stale copy, but protects that copy from
> GC. what a great thing! and all this without even a small warning from
> compiler. i can expect such behavior from c or c++ with all their UB,
> but not from D. this is not only ugly, this is plainly wrong.
>

No, it's not. It's exactly as you requested when you wrote that code :)

Note, you can fix this by overloading opAssign in Info, and it should 
work as you expect. This will force a delay in the evaluation of which 
array is used until *after* the RHS is done.

-Steve


More information about the Digitalmars-d mailing list