Struct should be invalid after move

Neia Neutuladh neia at ikeran.org
Wed Nov 28 20:21:42 UTC 2018


On Wed, 28 Nov 2018 14:11:14 -0500, Steven Schveighoffer wrote:
> Not sure what you mean. What I'm talking about is this:
> 
> int *ptr1 = (int *)malloc(sizeof(int));
> int *ptr2 = ptr1;
> 
> free(ptr2);
> 
> *ptr1 = 5;

Yes, and the compiler can't detect this very often (at least not without 
an ownership / borrowing system like Rust or Vala).

Instead, you can eliminate aliasing, introduce reference counting or a 
garbage collector to avoid manually freeing, or use indirection to detect 
when the value is freed.

There are a bunch of solutions that work some of the time. The end result 
is a bit of code that doesn't fit with any of it and needs special caution.


More information about the Digitalmars-d mailing list