escaping pointer to scope local array: bug or not?

Hosokawa Kenchi hskwk at inter7.jp
Mon Aug 17 13:51:20 PDT 2009


Robert Fraser Wrote:

> HOSOKAWA Kenchi wrote:
> > It seems dmd 2.031 forgets scope attribute for array.ptr in some cases, so that it allows escaping a pointer to scope local array.
> > I'm not sure this is a bug or a kind of "dangerous-but-valid".
>  >
> > int* ap()
> > {
> > 	scope auto a = new int[1];
> > 	return a.ptr; // no error; this is the problem
> > }
> 
> Probably should be an error, but, FWIW, scope arrays are still 
> heap-allocated (yeah, I know it's inconsistent). So there's no chance of 
> memory corruption, etc.; it's as if you didn't have the "scope" there.
> 

I'd tried to check where 'scoped' variables are on the Jacques's advice.
As a result, I reached the same conclusion to yours.
They are heap-allocated, of course, would not be collected by GC (I'm not sure collect-proofness is guaranteed or not).

I suppose semantics of 'scope' is still ambiguous.

int[] f() {
	scope x = new int[6];
	auto y = x[1..3];
	return y;			//	no error, successfully escape slice-reference of 'originally' scoped array.
}

I'm unable to make a quick decision that it SHOULD be error or not.
I wish here is a opinion which gives clear cut on this issue.



More information about the Digitalmars-d mailing list