Thread overview
void[] vs byte[]
Aug 28, 2010
Yao G.
Aug 28, 2010
BCS
August 28, 2010
I'm here with another n00b question:

When dealing with big buffers (or files), which is better to use as storage? void[] or byte[]?

What are the advantages and disadvantages of each one? I've seen that void[] is used in some Phobos modules, like std.zlib, and in other modules the choice is byte[] (chunk in std.stdio).

Is there a place where this stuff is documented? Or choosing one or another is just a matter of preference and the differences are trivial?

Thanks in advance.


-- 
Yao G.
August 28, 2010
Hello Yao G.,

> I'm here with another n00b question:
> 
> When dealing with big buffers (or files), which is better to use as
> storage? void[] or byte[]?

If the data may contain pointers into the heap, use void[] if it will not use byte[]. byte[] is "raw" data, void[] is anything at all.


> 
> What are the advantages and disadvantages of each one? I've seen that
> void[] is used in some Phobos modules, like std.zlib, and in other
> modules  the choice is byte[] (chunk in std.stdio).
> 
> Is there a place where this stuff is documented? Or choosing one or
> another is just a matter of preference and the differences are
> trivial?
> 
> Thanks in advance.
> 
-- 
... <IXOYE><



August 30, 2010
On Sat, 28 Aug 2010 16:13:28 -0400, BCS <none@anon.com> wrote:

> Hello Yao G.,
>
>> I'm here with another n00b question:
>>  When dealing with big buffers (or files), which is better to use as
>> storage? void[] or byte[]?
>
> If the data may contain pointers into the heap, use void[] if it will not use byte[]. byte[] is "raw" data, void[] is anything at all.

One other point to make -- any type of array casts to void[].  So void[] is kind of like a "catch all" array type.  It's typically the correct choice when accepting data that you are going to blindly copy somewhere.  For example a function to write data to a file.

For reading data/storing data, the best type might be ubyte[] (don't use byte[], it's signed).  void[] can also be used, but you may run into issues with "may contain pointers" problems.

I personally think the idea of allocating a void[] should set the "contains pointers" bit seems incorrect.  Usually when a void[] contains pointers, it was not allocated as a void[].

-Steve