Thread overview
0.93 bug with dyn array returns?
Jun 26, 2004
Miles
Jun 26, 2004
Derek
Jun 27, 2004
Serge Semashko
June 26, 2004
First post to digitalmars news... I'm really interested in D, but with a few minutes of programming, I found something that may be a bug. If I do:

	char[] test() {
		char[5] res = "12345";
		return res;
	}
	char[] result = test();

result receives right number of elements, but with garbage in their place. If I printf result, even more garbage is generated in it. If I change res in test() to char[], it returns what is expected to.

Looks like `return res;´ is returning a char[] structure with the right length field, but with a data pointer pointing to the stack within test(), which is invalidated after the function returns.

Sorry if this is a known issue.

-- 
Miles
June 26, 2004
On Sat, 26 Jun 2004 19:15:58 -0300, Miles wrote:

> First post to digitalmars news... I'm really interested in D, but with a few minutes of programming, I found something that may be a bug. If I do:
> 
> 	char[] test() {
> 		char[5] res = "12345";
> 		return res;
> 	}
> 	char[] result = test();
> 
> result receives right number of elements, but with garbage in their place. If I printf result, even more garbage is generated in it. If I change res in test() to char[], it returns what is expected to.
> 
> Looks like `return res;´ is returning a char[] structure with the right length field, but with a data pointer pointing to the stack within test(), which is invalidated after the function returns.
> 
> Sorry if this is a known issue.

You are correct that it is returning a pointer to a stack entity. The 'solution' is to return a copy of that array...

   return res.dup;

BTW, this is how D is expected to behave.

-- 
Derek
Melbourne, Australia
June 27, 2004
Derek wrote:

>>First post to digitalmars news... I'm really interested in D, but with a few minutes of programming, I found something that may be a bug. If I do:
>>
>>	char[] test() {
>>		char[5] res = "12345";
>>		return res;
>>	}
>>	char[] result = test();
>>
>>result receives right number of elements, but with garbage in their place. If I printf result, even more garbage is generated in it. If I change res in test() to char[], it returns what is expected to.
>>
>>Looks like `return res;´ is returning a char[] structure with the right length field, but with a data pointer pointing to the stack within test(), which is invalidated after the function returns.
>>
>>Sorry if this is a known issue.
> 
> 
> You are correct that it is returning a pointer to a stack entity. The
> 'solution' is to return a copy of that array...
> 
>    return res.dup;
> 
> BTW, this is how D is expected to behave.

Right, it is expected to behave so, but it is very nonobvious and almost every newbee hits this problem. Can D compiler detect this situation as compile time error (array allocated on stack as a return value) or add some runtime checks that can help debugging this?

Another nonobvious thing that a newbee can hit is the use of local string as associative array index, just try the following:

	char[] i;
	int[char[]] dict;
	i = "abc";
	dict[i] = 1;
	i = "def";
	dict[i] = 2;