November 30, 2005 Local variabled and nested functions | ||||
---|---|---|---|---|
| ||||
This small test-case: import std.stdio; int foo(int a) { int bar(int b) { a += b; if (b > 0) { return bar(b - 1); } else { return a; } } return bar(a); } int main(char[][] args) { writefln("a=", foo(3)); return 0; } Correctly gives "a=9" on gdc 0.15. But "a=72592" on gdc 0.16. Same goes for the smaller example (with purely local variable): int bar() { int a = 1; int baz(bool onemore) { if (onemore) return baz(false); else return a; } return baz(true); } Should always give 1, but mostly gives 72504 for me. I guess the pointer to the parents locals get screwed up. It works as long as I do not use recursion. regards Fredrik Olsson |
December 04, 2005 Re: Local variabled and nested functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Fredrik Olsson Attachments: | Fredrik Olsson schrieb am 2005-11-30: > This small test-case: > > import std.stdio; > > int foo(int a) { > int bar(int b) { > a += b; > if (b > 0) { > return bar(b - 1); > } else { > return a; > } > } > return bar(a); > } > > int main(char[][] args) { > writefln("a=", foo(3)); > return 0; > } > > Correctly gives "a=9" on gdc 0.15. But "a=72592" on gdc 0.16. > > Same goes for the smaller example (with purely local variable): > int bar() { > int a = 1; > int baz(bool onemore) { > if (onemore) > return baz(false); > else > return a; > } > return baz(true); > } > > Should always give 1, but mostly gives 72504 for me. I guess the pointer to the parents locals get screwed up. It works as long as I do not use recursion. > > regards > Fredrik Olsson Added to DStress as http://dstress.kuehne.cn/run/n/nested_function_07_A.d http://dstress.kuehne.cn/run/n/nested_function_07_B.d http://dstress.kuehne.cn/run/n/nested_function_07_C.d Thomas |
Copyright © 1999-2021 by the D Language Foundation