www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why does disabling a struct's postblit increase its size in memory?

reply Per =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
Why does disabling a struct's postblit increase its sizeof by one 
word?

The following holds:

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

. Why is this needed?
Mar 02
parent reply kinke <noone nowhere.com> writes:
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:

 ```d
 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.
Mar 02
parent reply Per =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
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.
Mar 02
parent reply Per =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
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.
Mar 02
parent Paul Backus <snarwin gmail.com> writes:
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: ```d 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 ```
Mar 03