Thread overview
Delegates with stackpointers
Sep 29, 2018
Ritchie
Sep 29, 2018
Alex
Sep 29, 2018
bauss
September 29, 2018
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?
September 29, 2018
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;
}
´´´
September 29, 2018
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