void[] vs ubyte[] - differences?

Dukc ajieskola at gmail.com
Thu Jul 17 18:56:08 UTC 2025


On Wednesday, 16 July 2025 at 13:29:01 UTC, z wrote:
> I also see this in the language documentation :
> ```
> A void array cannot be indexed.
> ```
> But i can slice it just fine in DMD 2.111...

You can _slice_ it, meaning, getting a subarray out of it. 
However, _indexing_ means getting a single element, which would 
be of type `void` if it was allowed.

> By extension what the differences between `void`, `ubyte`, 
> `void[]`, `ubyte[]` really?

`void[]` is supposed to be an array pointing to data of unknown 
type. `ubyte[]` is supposed to be pointing to array of data 
bytes. The latter might also be used to point to other data 
arrays, such as `wchar` arrays, but they are never intended for 
pointing to anything  that can't safely hold any binary value. 
So, if you have pointers, class references, classes, active 
reference counters or anything of that sort, it needs `void[]`.

The language-level differences that come to mind:

- You can read individual bytes of `ubyte[]` and change them. 
With `void[]` you cannot, unless you cast it to some other array 
type first.

- The garbage collector, or the compiler, does not have to treat 
data pointed to by `ubyte[]` as potential pointers. With 
`void[]`, the type isn't known so any optimisations or garbage 
collections have to assume the data might be pointers.

- You won't be able to do much anything with `void[]` in `@safe` 
code. `ubyte[]` is very `@safe` friendly though, since it is 
assumed there is no danger of overwriting pointers or other type 
safety critical data.




More information about the Digitalmars-d-learn mailing list