Thread overview
[Issue 20564] Member function increases struct size when the struct is defined in a function
Feb 07, 2020
Ferdinand Majerech
Feb 07, 2020
Simen Kjaeraas
Feb 07, 2020
Basile-z
Mar 21, 2020
Basile-z
February 07, 2020
https://issues.dlang.org/show_bug.cgi?id=20564

Ferdinand Majerech <kiithsacmp@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P1                          |P2
           Severity|regression                  |normal

--
February 07, 2020
https://issues.dlang.org/show_bug.cgi?id=20564

Simen Kjaeraas <simen.kjaras@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |simen.kjaras@gmail.com
         Resolution|---                         |DUPLICATE

--- Comment #1 from Simen Kjaeraas <simen.kjaras@gmail.com> ---
This is mostly expected. When a nested struct has a method, that method has access to the context of the function in which it is defined, so you can do things like this:

void fun() {
    int i = 0;
    struct S {
        void gun() { ++i; }
    }
    S s;
    s.gun();
    assert(i == 1);
}

Since the nested struct may be passed to other functions or even returned from the function that created it, this context pointer needs to be embedded in the struct, which explains the size increase - 1 byte for the explicit field, a context pointer of 8 bytes, and 7 bytes of padding to make sure the pointer is aligned.

Regardless, in your case the context pointer is strictly unnecessary, and could be elided. That makes this issue a duplicate of issue 10276.

*** This issue has been marked as a duplicate of issue 10276 ***

--
February 07, 2020
https://issues.dlang.org/show_bug.cgi?id=20564

Basile-z <b2.temp@gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |b2.temp@gmx.com

--- Comment #2 from Basile-z <b2.temp@gmx.com> ---
set the inner struct static when the context pointer is not required

void fun()
{
    static struct InnerFun
    {
        ubyte a;
        void fun() {}
    }
    static struct InnerNoFun
    {
        ubyte a;
    }
}

both have size 1 now.

--
March 21, 2020
https://issues.dlang.org/show_bug.cgi?id=20564

Basile-z <b2.temp@gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|b2.temp@gmx.com             |

--