www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - void[] and ubyte[]

reply "Stephan" <stephan_schiffels mac.com> writes:

In phobos there are places with void[] and places with ubyte[], 
and I can't really understand why there is this difference. I 
don't even see why D has a void array or void pointer anyway.
For example, in std.base64, ubyte[] is used as the basic quantity 
for encoding, while in std.zlib, it's void[]. Both libraries have 
a similar pair of functions like encode/decode or 
compress/uncompress. Both act on raw memory. So why not use 
ubyte[] all the time? Isn't any stretch of bare data on the heap 
in the end an array of bytes? Void[] seems like a C-anachronism 
to me. I have no experience with void pointers or arrays, so I am 
probably missing something.


Feb 04 2013
parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 2/5/13, Stephan <stephan_schiffels mac.com> wrote:
 In phobos there are places with void[] and places with ubyte[]
One benefit of void[] over ubyte[] is implicit conversion. E.g.: void input(void[] arr) { } void main() { input([1, 2]); input([1.0, 2.0]); input(["foo", "bar"]); } You don't get that conversion with byte arrays. And the benefit of byte[] over void[] is that (at least theoretically IIRC) the garbage collector won't scan byte arrays but it will scan void arrays. This means the GC could interpret an element in a void[] as a pointer and end up never releasing some bit of memory. Someone should expand on this better than me, but I think that's the gist of it.
Feb 04 2013