Cannot copy void[] to void[] in @safe code?
ag0aep6g
anonymous at example.com
Fri Jul 8 12:26:03 UTC 2022
On Friday, 8 July 2022 at 10:58:24 UTC, wjoe wrote:
> My understanding is that a void[] doesn't have a distinct type
> but since the length is bytes and not elements this makes me
> believe that under the hood they are byte arrays - or, rather,
> managed chunks of memory.
> How's copying memory without a distinct type different from
> copying, say, an ubyte[] to an ubyte[] ?
You're allowed to copy from `ubyte[]` to `ubyte[]`. But you're
not allowed to copy from `ubyte[]` to `int*[]`, because
reinterpreting a bunch of bytes as pointers is not safe.
The thing about `void[]` is that it can point to memory that also
has a typed alias. You can have a `void[]` and a `ubyte[]`
pointing to the same memory. And you can have a `void[]` and an
`int*[]` pointing to the same memory. So if you were allowed to
copy from `void[]` to `void[]`, you'd practically be allowed to
copy from `ubyte[]` to `int*[]`. But that's not safe.
In code:
```d
void main() @safe
{
ubyte[] some_bytes = [1, 2, 3, 4, 5, 6, 7, 8];
int*[] some_pointers = [new int];
void[] v1 = some_bytes;
void[] v2 = some_pointers;
v2[] = v1[]; /* creating invalid pointers */
int x = *some_pointers[0]; /* undefined behavior */
}
```
More information about the Digitalmars-d-learn
mailing list