std.file.read returns void[] why?

monarch_dodra via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Apr 17 23:04:16 PDT 2014


On Thursday, 17 April 2014 at 21:27:44 UTC, Steven Schveighoffer 
wrote:
> On Thu, 17 Apr 2014 17:04:25 -0400, monarch_dodra 
> <monarchdodra at gmail.com> wrote:
>> void[] will only make sense once you've accepted that 
>> "void.sizeof == 1".
>
> It is already accepted that when we talk about length in a 
> void[], it's the number of bytes. But the data has no formal 
> type.

Well, I always thought that "void[] slice" meant "there are 
slice.length items, starting at slice.ptr. I don't know the size 
of the individual items".

For example, in C, a lot of functions take "void* first, size_t 
num, size_t width".

In fact, most of druntime functions take "void[]" buffers that 
work that way. There's an associated typeid, so that you can now 
how large each individual items are.

> But any array implicitly casts to void[]. This is why it makes 
> a good parameter for read or write (when reading or writing the 
> binary data).

I guess. I just find it kind of strange that a type "that has no 
type" would have an actual sizeof. Then again, I thought void had 
no sizeof in C, but I just checked, and I was wrong.

>> Well, I guess "void[]" is C++'s "char*" for indiscriminate 
>> buffers. Speaking of which, does "void*" trigger strict 
>> aliasing in D? This subject seems like a hot potato no-one 
>> wants to touch.
>
> No, it's equivalent to void *, not char *.
>
> in D, ubyte[] would be the equivalent of C's char *.
>
> -Steve

Correct.


More information about the Digitalmars-d-learn mailing list