www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Delegates with stackpointers

reply Ritchie <ritchie shark.at> writes:
How does a delegate with a stackpointer work? e.g. in this 
example:

https://run.dlang.io/is/XviMSl

Does the second call to foo not overwrite the stack of the first 
call and thereby the data pointed to by bar1? How is that data 
preserved?
Sep 28 2018
next sibling parent Alex <sascha.orlov gmail.com> writes:
On Saturday, 29 September 2018 at 06:01:50 UTC, Ritchie wrote:
 How does a delegate with a stackpointer work? e.g. in this 
 example:

 https://run.dlang.io/is/XviMSl

 Does the second call to foo not overwrite the stack of the 
 first call and thereby the data pointed to by bar1? How is that 
 data preserved?
Why should a call to foo overwrite something? Aren't these two foos equivalent from this point of view? ´´´ DelegateT foo1() { int a = 0; void bar() { a++; writeln(a); } return &bar; } auto foo2() { struct S { int a; void opCall() { a++; writeln(a); } } return S.init; } ´´´
Sep 29 2018
prev sibling parent bauss <jj_1337 live.dk> writes:
On Saturday, 29 September 2018 at 06:01:50 UTC, Ritchie wrote:
 How does a delegate with a stackpointer work? e.g. in this 
 example:

 https://run.dlang.io/is/XviMSl

 Does the second call to foo not overwrite the stack of the 
 first call and thereby the data pointed to by bar1? How is that 
 data preserved?
In this case "a" will not live on the stack of "foo". This can be proved by: Shows all variables in "sequence: (Because "a" is not used within the delegate.) https://run.dlang.io/is/mwopBi Shows only "b" and "c" in sequence: https://run.dlang.io/is/c0rpO8
Sep 29 2018