RCArray is unsafe
Andrei Alexandrescu via Digitalmars-d
digitalmars-d at puremagic.com
Wed Mar 4 07:48:40 PST 2015
On 3/4/15 2:03 AM, deadalnix wrote:
> A free list does not work as the data can be live. You cannot reuse it
> to maintain the free list.
Actually you can, which is a bit surprising. Consider:
RCArray!int arr;
arr.length = 10;
arr[5] = 42;
fun(arr, arr[5]);
...
void fun(ref RCArray!int a, ref int b) {
assert(b == 42);
a = a.init; // array goes to freelist
a.length = 10; // array may reuse the previous one!
assert(b == 42);
}
Now the interesting here thing is, the last assert should be allowed to
fail. The code is still safe because there is no change of type, and the
use of b after the array is gone is a bug in the application anyway
because the parent structure is out of existence.
This makes code potentially tighter on memory usage but potentially more
difficult to debug.
Andrei
More information about the Digitalmars-d
mailing list