www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - void[] or ubyte[] ?

reply derick_eddington nospam.dot.yahoo.dot.com writes:
This probably has been asked before... but maybe other new people will learn
too.

I've been coding stuff that involves a lot of raw bytes-as-basic-storage-unit
and wondering about using void[] vs ubyte[].  Technically, a ubyte is an 8-bit
unsigned integer while a void[] element is a unit of the address-space i.e. an
8-bit block, right?  For byte-storage-unit usage they're functionally the same?
So what is the correct one to use when you want to deal with blocks/streams of
the computer's basic storage unit, the 8-bit byte?  I've been using void[] but
I've seen ubyte[] used a lot as well.
Mar 27 2005
next sibling parent =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
derick_eddington wrote:

 I've been coding stuff that involves a lot of raw bytes-as-basic-storage-unit
 and wondering about using void[] vs ubyte[].  Technically, a ubyte is an 8-bit
 unsigned integer while a void[] element is a unit of the address-space i.e. an
 8-bit block, right?  For byte-storage-unit usage they're functionally the same?
 So what is the correct one to use when you want to deal with blocks/streams of
 the computer's basic storage unit, the 8-bit byte?  I've been using void[] but
 I've seen ubyte[] used a lot as well.
void[] is good for binary storage, *unless* you want to index it... I've been using ubyte[] for storing 8-bit character encodings, but for storing something like raw binary data - one might as well use void[] ? So ubyte[] is good for what used to be (char*), and void[] for (void*) --anders
Mar 28 2005
prev sibling parent reply "Lionello Lunesu" <lio lunesu.removethis.com> writes:
But.. What does .length mean for a void[]?
for all other arrays, .length is the number of elements: for int[] the 
number of ints, for byte[] the number of bytes, for void the number of 
voids??

If you want .length to mean 'number of bytes', why not use byte[]? void* 
does not have this problem: it's simply a handle to something, but void[] 
makes no sense: you know what it contains or else you wouldn't know how many 
of 'em you had?!

Why is void[] even allowed? Please enlighten me.

Lionello.

<derick_eddington nospam.dot.yahoo.dot.com> wrote in message 
news:d282dn$svc$1 digitaldaemon.com...
 This probably has been asked before... but maybe other new people will 
 learn
 too.

 I've been coding stuff that involves a lot of raw 
 bytes-as-basic-storage-unit
 and wondering about using void[] vs ubyte[].  Technically, a ubyte is an 
 8-bit
 unsigned integer while a void[] element is a unit of the address-space 
 i.e. an
 8-bit block, right?  For byte-storage-unit usage they're functionally the 
 same?
 So what is the correct one to use when you want to deal with 
 blocks/streams of
 the computer's basic storage unit, the 8-bit byte?  I've been using void[] 
 but
 I've seen ubyte[] used a lot as well.


 
Mar 30 2005
parent "Regan Heath" <regan netwin.co.nz> writes:
IIRC Walter once admitted that void.sizeof was equal to 1 and that was  
required for void[] to work. You're asking why void[] is required.. the  
only answer I have is this:

import std.stdio;

void foov(void[] a) {}
void fooi(int[] a) {}
void foos(short[] a) {}
void foob(byte[] a) {}

void main()
{
	int[] iarr;
	short[] sarr;
	byte[] barr;
	
	writefln(void.sizeof);
	foov(iarr); //ok
	foov(sarr); //ok
	foov(barr); //ok
	
	fooi(iarr); //ok
	fooi(sarr); //error
	fooi(barr); //error

	foos(iarr); //error
	foos(sarr); //ok
	foos(barr); //error

	foob(iarr); //error
	foob(sarr); //error
	foob(barr); //ok
}

it appears to me that void[] is different to byte[] in one particular way,  
every array type (that I've tested anyway) is implicitly castable to  
void[].

My question is, if this is the reason for void[], is there a reason byte[]  
couldn't have this special behaviour instead?

Regan

On Thu, 31 Mar 2005 09:39:44 +0300, Lionello Lunesu  
<lio lunesu.removethis.com> wrote:
 But.. What does .length mean for a void[]?
 for all other arrays, .length is the number of elements: for int[] the
 number of ints, for byte[] the number of bytes, for void the number of
 voids??

 If you want .length to mean 'number of bytes', why not use byte[]? void*
 does not have this problem: it's simply a handle to something, but void[]
 makes no sense: you know what it contains or else you wouldn't know how  
 many
 of 'em you had?!

 Why is void[] even allowed? Please enlighten me.

 Lionello.

 <derick_eddington nospam.dot.yahoo.dot.com> wrote in message
 news:d282dn$svc$1 digitaldaemon.com...
 This probably has been asked before... but maybe other new people will
 learn
 too.

 I've been coding stuff that involves a lot of raw
 bytes-as-basic-storage-unit
 and wondering about using void[] vs ubyte[].  Technically, a ubyte is an
 8-bit
 unsigned integer while a void[] element is a unit of the address-space
 i.e. an
 8-bit block, right?  For byte-storage-unit usage they're functionally  
 the
 same?
 So what is the correct one to use when you want to deal with
 blocks/streams of
 the computer's basic storage unit, the 8-bit byte?  I've been using  
 void[]
 but
 I've seen ubyte[] used a lot as well.
Mar 31 2005