Implicit @Safe Resources

Menshikov mensikovk817 at gmail.com
Sun Sep 3 14:42:07 UTC 2023


On Sunday, 3 September 2023 at 14:19:55 UTC, Richard (Rikki) 
Andrew Cattermole wrote:
> On 04/09/2023 2:15 AM, Menshikov wrote:
>> We have an unnecessary memory overhead. 2 bytes became 4 bytes.
>
> A uint is 4 bytes. So it would be 4 to 8 on 32bits. On 64bit 
> that would be 16 bytes due to alignment.
>
> But it doesn't have to be wasteful.
>
> ```d
> union {
> 	uint id;
> 	void* ptr;
> }
> ```

ABI doesn't work that way, alignment happens over a larger field 
in the structure.

```d
struct SafeRef {uint id;}

struct PtrHack {
     union {
     	uint id;
         void* ptr;
     }
}

pragma(msg, SafeRef.sizeof); // 4
pragma(msg, PtrHack.sizeof); // 8

void main(){}
```


More information about the Digitalmars-d mailing list