Thread overview | |||||
---|---|---|---|---|---|
|
June 26, 2004 0.93 bug with dyn array returns? | ||||
---|---|---|---|---|
| ||||
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 Re: 0.93 bug with dyn array returns? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Miles | 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 Re: 0.93 bug with dyn array returns? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Derek | 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;
|
Copyright © 1999-2021 by the D Language Foundation