DIP1000: Scoped Pointers (Discussion)

H. S. Teoh via Digitalmars-d digitalmars-d at puremagic.com
Fri Aug 12 09:03:03 PDT 2016


On Fri, Aug 12, 2016 at 01:27:08PM +0000, Kagamin via Digitalmars-d wrote:
> On Thursday, 11 August 2016 at 21:57:06 UTC, Walter Bright wrote:
> > > auto arr = RefCountedSlice!int(10);
> > > auto ptr = &arr[5];
> > > arr = RefCountedSlice!int(42);
> > > *ptr = 1;    // use after free
> > 
> > The idea is to have containers return references by 'return ref' or
> > 'return scope' so the internal references can't escape the
> > expression they're used in.
> 
> What expressions are allowed? Will this work?
> 
> auto arr = RefCountedSlice!int(10);
> void f(scope ref int n)
> {
>   arr = RefCountedSlice!int(42);
>   n = 1; // use after free
> }
> f(arr[5]);

Nice one!

But I believe the current rules ought to catch this case as well,
because if RefCountedSlice!int(42) returns by scope, then assigning it
to arr would be illegal (lifetime of arr is longer than lifetime of the
new slice).


T

-- 
"No, John.  I want formats that are actually useful, rather than over-featured megaliths that address all questions by piling on ridiculous internal links in forms which are hideously over-complex." -- Simon St. Laurent on xml-dev


More information about the Digitalmars-d mailing list