First Draft: cast(ref T)... as shorthand for *cast(T*)&...
Walter Bright
newshound2 at digitalmars.com
Wed Jan 22 01:01:58 UTC 2025
On 1/21/2025 1:33 PM, IchorDev wrote:
> On Monday, 20 January 2025 at 23:09:24 UTC, Walter Bright wrote:
>> I haven't really thought about that. But this also works and is simpler and
>> easier to read:
>>
>> ```
>> void[] foo = cast(void[])((&bar)[0 .. 1]);
>> ```
>
> Doesn’t this incur a runtime bounds check?
No.
> Also does this work with non-void slices?
It is a non-void slice!
>> That wouldn't be good because it would stomp on whatever is next in memory.
>
> No, it would allocate new stack memory or use a new register.
That could happen, but it is not what happens.
> You’ll assign the
> expression to something anyway, so it’s like if you declared a default
> initialised variable and partially overwrote it. I’ve actually needed this
> pattern quite a lot when interacting with C and often I just have to cast to
> `ubyte[]` and write all the lengths manually. Looks ugly.
Reading past the end of a memory object could produce a seg fault. Writing is a
near certain disaster.
More information about the dip.development
mailing list