Gc/D_runtime prevents dangling pointers?

thedeemon dlang at thedeemon.com
Thu Jan 4 11:50:48 UTC 2018


On Thursday, 4 January 2018 at 11:05:25 UTC, tipdbmp wrote:
>> What is your definition of a dangling pointer?
> A pointer pointing to freed memory, which presumably '&a[0]' 
> should be because it reallocates.

It allocates a larger array, but the old version is not freed up 
front. Right because there might be live references to the old 
data. Your pointer is such live reference. So while this pointer 
references that old version of the array, it's not freed. Only 
after no references are left the GC will make it free (and only 
when next GC cycle happens, not immediately).

> It seems that the '~=' operator "knows" that there's a 
> reference to 'a's old memory and it keeps it around instead of 
> freeing it.

It's just not its job to free that memory. That memory is freed 
later by GC, when it's safe to do so.


More information about the Digitalmars-d-learn mailing list