digitalmars.D.bugs - variable scope
- Miguel Ferreira Simões (9/9) Apr 11 2005 Is this a bug?
- Ben Hinkle (5/12) Apr 11 2005 no - the stack of test(0), which contains num, is gone by the time the
- Miguel Ferreira Simões (1/4) Apr 11 2005 You are right! Thanks!
- Manfred Nowak (21/23) Apr 11 2005 According to the specs this behaviour _is_ buggy, because the specs
- Ben Hinkle (10/32) Apr 11 2005 I'm not sure what section you are looking at but if you have a suggestio...
- Sean Kelly (4/19) Apr 11 2005 Really? But the delegate is called before the function exits. I would ...
- Sean Kelly (4/25) Apr 11 2005 I take it back. I misread the return value for test. I thought it retu...
Is this a bug? int delegate () test(int num) { return delegate int() { return num; }; } writefln("num: ", test(0)()); -- Miguel Ferreira Simões
Apr 11 2005
"Miguel Ferreira Simões" <Kobold netcabo.pt> wrote in message news:d3dopj$28a2$1 digitaldaemon.com...Is this a bug? int delegate () test(int num) { return delegate int() { return num; }; } writefln("num: ", test(0)());no - the stack of test(0), which contains num, is gone by the time the delegate is called. You need to store num on the heap if you need it to live beyond the call to test(0).
Apr 11 2005
no - the stack of test(0), which contains num, is gone by the time the delegate is called. You need to store num on the heap if you need it to live beyond the call to test(0).You are right! Thanks!
Apr 11 2005
"Ben Hinkle" <ben.hinkle gmail.com> wrote: [...]the stack of test(0), which contains num, is gone by the time the delegate is called.According to the specs this behaviour _is_ buggy, because the specs only make accesses to local _variables_ illegal, not to parameters. But how about this: <code> import std.stdio; void main(){ int x=0; int delegate () y; int delegate () test(inout int num) { y= delegate int() { return num; }; return y; } writefln("num: ", test(x)()); } </code> Still wrong result. -manfred
Apr 11 2005
"Manfred Nowak" <svv1999 hotmail.com> wrote in message news:d3e3vo$2mqn$1 digitaldaemon.com..."Ben Hinkle" <ben.hinkle gmail.com> wrote: [...]I'm not sure what section you are looking at but if you have a suggestion for clarifying the doc I'd add a note to the wiki for documentation feedback. About the specific issue I doubt Walter intends for D to have different lifetimes for local variables and parameters.the stack of test(0), which contains num, is gone by the time the delegate is called.According to the specs this behaviour _is_ buggy, because the specs only make accesses to local _variables_ illegal, not to parameters.But how about this: <code> import std.stdio; void main(){ int x=0; int delegate () y; int delegate () test(inout int num) { y= delegate int() { return num; }; return y; } writefln("num: ", test(x)()); } </code> Still wrong result.Correct. The delegate is still accessing the parameter "num". It doesn't matter that num is a pointer or reference to another location. To work properly the delegate can't refer to anything that is not on the stack when the delegate is called.
Apr 11 2005
In article <d3dpvd$29n6$1 digitaldaemon.com>, Ben Hinkle says..."Miguel Ferreira Simões" <Kobold netcabo.pt> wrote in message news:d3dopj$28a2$1 digitaldaemon.com...Really? But the delegate is called before the function exits. I would expect the stack frame to be intact at this point. SeanIs this a bug? int delegate () test(int num) { return delegate int() { return num; }; } writefln("num: ", test(0)());no - the stack of test(0), which contains num, is gone by the time the delegate is called. You need to store num on the heap if you need it to live beyond the call to test(0).
Apr 11 2005
In article <d3ehk4$61r$1 digitaldaemon.com>, Sean Kelly says...In article <d3dpvd$29n6$1 digitaldaemon.com>, Ben Hinkle says...I take it back. I misread the return value for test. I thought it returned an int, not a delegate. Sean"Miguel Ferreira Simões" <Kobold netcabo.pt> wrote in message news:d3dopj$28a2$1 digitaldaemon.com...Really? But the delegate is called before the function exits. I would expect the stack frame to be intact at this point.Is this a bug? int delegate () test(int num) { return delegate int() { return num; }; } writefln("num: ", test(0)());no - the stack of test(0), which contains num, is gone by the time the delegate is called. You need to store num on the heap if you need it to live beyond the call to test(0).
Apr 11 2005