escaping pointer to scope local array: bug or not?

Robert Jacques sandford at jhu.edu
Sun Aug 16 10:20:32 PDT 2009


On Sun, 16 Aug 2009 10:13:42 -0700, HOSOKAWA Kenchi <hskwk at inter7.jp>  
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[] a()
> {
> 	scope auto a = new int[1];
> 	return a; // error; escaping reference to scope local array
> }
>
> int* ap()
> {
> 	scope auto a = new int[1];
> 	return a.ptr; // no error; this is the problem
> }
>
> int* i()
> {
> 	int i;
> 	return &i; // error; escaping reference to local variable
> }
>
> int* ip()
> {
> 	scope int* p = new int;
> 	return p; // no error; only is "int* p" local, "new int" not scope  
> local?
> }
>

I'd recommend checking to see if p* was allocated on the stack or on the  
heap, as the difference represents two very different bugs.



More information about the Digitalmars-d mailing list