Thread overview
Why does disabling a struct's postblit increase its size in memory?
Mar 02
kinke
March 02

Why does disabling a struct's postblit increase its sizeof by one word?

The following holds:

struct S { @disable this(this); int _; }
struct T { int _; }
static assert(S.sizeof == 16);
static assert(T.sizeof == int.sizeof);

. Why is this needed?

March 02

On Saturday, 2 March 2024 at 15:25:48 UTC, Per Nordlöw wrote:

>

Why does disabling a struct's postblit increase its sizeof by one word?

The following holds:

struct S { @disable this(this); int _; }
struct T { int _; }
static assert(S.sizeof == 16);
static assert(T.sizeof == int.sizeof);

Not according to run.dlang.io, for all available DMD versions. Perhaps your tested S was nested in some function/aggregate and so had an implicit context pointer.

March 02

On Saturday, 2 March 2024 at 19:11:42 UTC, kinke wrote:

>

Not according to run.dlang.io, for all available DMD versions. Perhaps your tested S was nested in some function/aggregate and so had an implicit context pointer.

Ahh. Yes. Indeed. My mistake. Thanks.

March 02

On Saturday, 2 March 2024 at 19:28:08 UTC, Per Nordlöw wrote:

>

On Saturday, 2 March 2024 at 19:11:42 UTC, kinke wrote:

>

Not according to run.dlang.io, for all available DMD versions. Perhaps your tested S was nested in some function/aggregate and so had an implicit context pointer.

Ahh. Yes. Indeed. My mistake. Thanks.

Thanks. Neither my websearches nor ChatGPT plus couldn't figure that out.

March 03

On Saturday, 2 March 2024 at 19:29:47 UTC, Per Nordlöw wrote:

>

On Saturday, 2 March 2024 at 19:28:08 UTC, Per Nordlöw wrote:

>

On Saturday, 2 March 2024 at 19:11:42 UTC, kinke wrote:

>

Not according to run.dlang.io, for all available DMD versions. Perhaps your tested S was nested in some function/aggregate and so had an implicit context pointer.

Ahh. Yes. Indeed. My mistake. Thanks.

Thanks. Neither my websearches nor ChatGPT plus couldn't figure that out.

FYI, you can dump the layout of a struct, including hidden fields, by iterating over its .tupleof property:

void main()
{
    struct S
    {
        @disable this(this);
        int n;
    }

    static foreach (field; S.tupleof)
        pragma(msg,
            typeof(field).stringof, " ",
            __traits(identifier, field), " ",
            "at ", field.offsetof
        );
}

This example prints out

int n at 0LU
void* this at 8LU