www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - variable scope

reply "Miguel Ferreira Simões" <Kobold netcabo.pt> writes:
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
parent reply "Ben Hinkle" <ben.hinkle gmail.com> writes:
"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
next sibling parent "Miguel Ferreira Simões" <Kobold netcabo.pt> writes:
 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
prev sibling next sibling parent reply Manfred Nowak <svv1999 hotmail.com> writes:
"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
parent "Ben Hinkle" <ben.hinkle gmail.com> writes:
"Manfred Nowak" <svv1999 hotmail.com> wrote in message 
news:d3e3vo$2mqn$1 digitaldaemon.com...
 "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.
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.
 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
prev sibling parent reply Sean Kelly <sean f4.ca> writes:
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...
 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).
Really? But the delegate is called before the function exits. I would expect the stack frame to be intact at this point. Sean
Apr 11 2005
parent Sean Kelly <sean f4.ca> writes:
In article <d3ehk4$61r$1 digitaldaemon.com>, Sean Kelly says...
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...
 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).
Really? But the delegate is called before the function exits. I would expect the stack frame to be intact at this point.
I take it back. I misread the return value for test. I thought it returned an int, not a delegate. Sean
Apr 11 2005