www.digitalmars.com         C & C++   DMDScript  

D - void no type

reply "Phill" <phill pacific.net.au> writes:
Ive got a (probably)dumb question here.

What is a void? I thought it was nothing.
It seems that in D it has a new meaning.

For example, how do I call this method?

aMethod(void[] buf)
{
}

It seems to me that I should need a char[]

I had a quick look at  the digtalsmars/d
but couldnt see it, and URL's or quick
tutorials via here, would be great :o))

Thanks for any help.

Phill.
Mar 04 2004
next sibling parent Matthias Becker <Matthias_member pathlink.com> writes:
Ive got a (probably)dumb question here.

What is a void? I thought it was nothing.
It seems that in D it has a new meaning.

For example, how do I call this method?

aMethod(void[] buf)
{
}
Interesting. In C a pointer to void was used for some kind of generic programming. E.g. malloc/free (C's way to dynamically (de)allocate memory) used this: void * malloc (size_t); void free (void * ptr); Type * memory = (Type *)malloc(size); .. free (memory); But I don't know what void-arrays could be used for in D.
Mar 04 2004
prev sibling next sibling parent reply C <dont respond.com> writes:
Where do you see this method ?  You cant have an array of void , but =

usually 'void *' just means ( a chunk of data, no specified type ).  It'=
s =

useful for manipulating raw data, coming in from sockets for example.  =

Marking a function as returning void means return nothing ( it is =

confusing now that u mention it, its just been around so long nobody =

thinks about it :) )

C

On Thu, 4 Mar 2004 20:22:17 +1100, Phill <phill pacific.net.au> wrote:

 Ive got a (probably)dumb question here.

 What is a void? I thought it was nothing.
 It seems that in D it has a new meaning.

 For example, how do I call this method?

 aMethod(void[] buf)
 {
 }

 It seems to me that I should need a char[]

 I had a quick look at  the digtalsmars/d
 but couldnt see it, and URL's or quick
 tutorials via here, would be great :o))

 Thanks for any help.

 Phill.
-- = Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Mar 04 2004
parent "Phill" <phill pacific.net.au> writes:
yep Im aware of void (nothing) :o))
Its just an array of nothing that had me confused

Phill.


"C" <dont respond.com> wrote in message news:opr4crhxpfehmtou localhost...
Where do you see this method ?  You cant have an array of void , but
usually 'void *' just means ( a chunk of data, no specified type ).  It's
useful for manipulating raw data, coming in from sockets for example.
Marking a function as returning void means return nothing ( it is
confusing now that u mention it, its just been around so long nobody
thinks about it :) )

C

On Thu, 4 Mar 2004 20:22:17 +1100, Phill <phill pacific.net.au> wrote:

 Ive got a (probably)dumb question here.

 What is a void? I thought it was nothing.
 It seems that in D it has a new meaning.

 For example, how do I call this method?

 aMethod(void[] buf)
 {
 }

 It seems to me that I should need a char[]

 I had a quick look at  the digtalsmars/d
 but couldnt see it, and URL's or quick
 tutorials via here, would be great :o))

 Thanks for any help.

 Phill.
-- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Mar 04 2004
prev sibling next sibling parent reply Vathix <vathix dprogramming.com> writes:
Phill wrote:

 Ive got a (probably)dumb question here.
 
 What is a void? I thought it was nothing.
 It seems that in D it has a new meaning.
 
 For example, how do I call this method?
 
 aMethod(void[] buf)
 {
 }
 
 It seems to me that I should need a char[]
 
 I had a quick look at  the digtalsmars/d
 but couldnt see it, and URL's or quick
 tutorials via here, would be great :o))
 
 Thanks for any help.
 
 Phill.
 
It's actually a nice feature that allows you to have an array of any type, for example: int[1] test; void[] v = test; printf("v.length = %d", v.length); Prints out 4, without needing to cast or do length * size math. I used void[] in the send and receive methods of my Socket class. -- Christopher E. Miller
Mar 04 2004
next sibling parent reply C <dont respond.com> writes:
Hmm, I didnt know you could have an array of void.  I distinctly remembe=
r =

the compielr telling me I couldn't.  My mistake :S.

C

On Thu, 04 Mar 2004 17:59:05 -0500, Vathix <vathix dprogramming.com> wro=
te:

 Phill wrote:

 Ive got a (probably)dumb question here.

 What is a void? I thought it was nothing.
 It seems that in D it has a new meaning.

 For example, how do I call this method?

 aMethod(void[] buf)
 {
 }

 It seems to me that I should need a char[]

 I had a quick look at  the digtalsmars/d
 but couldnt see it, and URL's or quick
 tutorials via here, would be great :o))

 Thanks for any help.

 Phill.
It's actually a nice feature that allows you to have an array of any =
 type, for example:

 int[1] test;
 void[] v =3D test;
 printf("v.length =3D %d", v.length);

 Prints out 4, without needing to cast or do length * size math. I used=
=
 void[] in the send and receive methods of my Socket class.
-- = Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Mar 04 2004
parent Vathix <vathix dprogramming.com> writes:
C wrote:

 Hmm, I didnt know you could have an array of void.  I distinctly 
 remember the compielr telling me I couldn't.  My mistake :S.
 
 C
 
It's not like a regular array though; since it has no defined type, you can't index or slice it. Maybe that was the error you got. -- Christopher E. Miller
Mar 04 2004
prev sibling next sibling parent "Phill" <phill pacific.net.au> writes:
"Vathix" <vathix dprogramming.com> wrote in message
news:c28cbo$1oom$1 digitaldaemon.com...
 Phill wrote:

 Ive got a (probably)dumb question here.

 What is a void? I thought it was nothing.
 It seems that in D it has a new meaning.

 For example, how do I call this method?

 aMethod(void[] buf)
 {
 }

 It seems to me that I should need a char[]

 I had a quick look at  the digtalsmars/d
 but couldnt see it, and URL's or quick
 tutorials via here, would be great :o))

 Thanks for any help.

 Phill.
It's actually a nice feature that allows you to have an array of any type, for example: int[1] test; void[] v = test; printf("v.length = %d", v.length); Prints out 4, without needing to cast or do length * size math. I used void[] in the send and receive methods of my Socket class.
Yes thanks, very much, that is exactly where I have seen it. So I take it I can use a char[] instead of your int example? Thanks Phill.
 --
 Christopher E. Miller
Mar 04 2004
prev sibling parent reply "Phill" <phill pacific.net.au> writes:
"Vathix" <vathix dprogramming.com> wrote in message
news:c28cbo$1oom$1 digitaldaemon.com...
 Phill wrote:

 Ive got a (probably)dumb question here.

 What is a void? I thought it was nothing.
 It seems that in D it has a new meaning.

 For example, how do I call this method?

 aMethod(void[] buf)
 {
 }

 It seems to me that I should need a char[]

 I had a quick look at  the digtalsmars/d
 but couldnt see it, and URL's or quick
 tutorials via here, would be great :o))

 Thanks for any help.

 Phill.
It's actually a nice feature that allows you to have an array of any type, for example: int[1] test; void[] v = test; printf("v.length = %d", v.length);
snip From that code : test.length would equal 1 and v.length would equal 4; Do you agree that this is inconsistent? Is there a reason for this, or is it a bug(which I doubt)? Phill.
 --
 Christopher E. Miller
Mar 05 2004
parent reply Vathix <vathix dprogramming.com> writes:
Phill wrote:

 "Vathix" <vathix dprogramming.com> wrote in message
 news:c28cbo$1oom$1 digitaldaemon.com...

 Phill wrote:


 Ive got a (probably)dumb question here.

 What is a void? I thought it was nothing.
 It seems that in D it has a new meaning.

 For example, how do I call this method?

 aMethod(void[] buf)
 {
 }

 It seems to me that I should need a char[]

 I had a quick look at  the digtalsmars/d
 but couldnt see it, and URL's or quick
 tutorials via here, would be great :o))

 Thanks for any help.

 Phill.
It's actually a nice feature that allows you to have an array of any type, for example: int[1] test; void[] v = test; printf("v.length = %d", v.length);
snip From that code : test.length would equal 1 and v.length would equal 4; Do you agree that this is inconsistent? Is there a reason for this, or is it a bug(which I doubt)? Phill.
Not really inconsistent. This gives the same output, just requires the cast: int[1] test; byte[] v = cast(byte[])test; printf("v.length = %d", v.length); -- Christopher E. Miller
Mar 05 2004
parent reply "Phill" <phill pacific.net.au> writes:
It seems to me that when calling ".length" on
an int[], it is reffering to the amount of indexes
in the array, which is consistent with the Array Properties on this page:
http://www.digitalmars.com/d/arrays.html

but the void[] and now I see the byte[] seem
to be reffering to somthing completely different,
when using ".length" on them. Which seems to
be somthing similar to what ".size"  returns at the previous URL that I
mentioned.

ie:

void[] v = test;
byte[] b = cast(byte[])test;
printf("test.length = %d", test.length); //test.length = 8
printf("test.size = %d", test.size); // test.size = 32
printf("v.length = %d", v.length); // v.length = 32
printf("b.length = %d", b.length); //b.length = 32

Can you correct me if I am wrong?

Phill.

"Vathix" <vathix dprogramming.com> wrote in message
news:c2a3e1$1mqj$2 digitaldaemon.com...
 Phill wrote:

  > "Vathix" <vathix dprogramming.com> wrote in message
  > news:c28cbo$1oom$1 digitaldaemon.com...
  >
  >> Phill wrote:
  >>
  >>
  >>> Ive got a (probably)dumb question here.
  >>>
  >>> What is a void? I thought it was nothing.
  >>> It seems that in D it has a new meaning.
  >>>
  >>> For example, how do I call this method?
  >>>
  >>> aMethod(void[] buf)
  >>> {
  >>> }
  >>>
  >>> It seems to me that I should need a char[]
  >>>
  >>> I had a quick look at  the digtalsmars/d
  >>> but couldnt see it, and URL's or quick
  >>> tutorials via here, would be great :o))
  >>>
  >>> Thanks for any help.
  >>>
  >>> Phill.
  >>>
  >>
  >> It's actually a nice feature that allows you to have an array of any
  >> type, for example:
  >>
  >> int[1] test;
  >> void[] v = test;
  >> printf("v.length = %d", v.length);
  >>
  >
  > snip
  >
  > From that code :
  >
  > test.length  would equal 1 and
  > v.length would equal 4;
  >
  > Do  you agree that this is inconsistent?
  > Is there a reason for this, or is it a bug(which I doubt)?
  >
  > Phill.


 Not really inconsistent. This gives the same output, just requires the
cast:
 int[1] test;
 byte[] v = cast(byte[])test;
 printf("v.length = %d", v.length);

 --
 Christopher E. Miller
Mar 05 2004
parent reply "Phill" <phill pacific.net.au> writes:
That should have been:


 but the void[] and now I see the byte[] seem
 to be reffering to somthing completely different,
 when using ".length" on them. Which seems to
 be somthing similar to what ".size"  returns at the previous URL that I
 mentioned.

 ie:


 int[8] test;
 void[] v = test;
 byte[] b = cast(byte[])test;
 printf("test.length = %d", test.length); //test.length = 8
 printf("test.size = %d", test.size); // test.size = 32
 printf("v.length = %d", v.length); // v.length = 32
 printf("b.length = %d", b.length); //b.length = 32
 Can you correct me if I am wrong?

 Phill.
 "Vathix" <vathix dprogramming.com> wrote in message
 news:c2a3e1$1mqj$2 digitaldaemon.com...
 Phill wrote:

  > "Vathix" <vathix dprogramming.com> wrote in message
  > news:c28cbo$1oom$1 digitaldaemon.com...
  >
  >> Phill wrote:
  >>
  >>
  >>> Ive got a (probably)dumb question here.
  >>>
  >>> What is a void? I thought it was nothing.
  >>> It seems that in D it has a new meaning.
  >>>
  >>> For example, how do I call this method?
  >>>
  >>> aMethod(void[] buf)
  >>> {
  >>> }
  >>>
  >>> It seems to me that I should need a char[]
  >>>
  >>> I had a quick look at  the digtalsmars/d
  >>> but couldnt see it, and URL's or quick
  >>> tutorials via here, would be great :o))
  >>>
  >>> Thanks for any help.
  >>>
  >>> Phill.
  >>>
  >>
  >> It's actually a nice feature that allows you to have an array of any
  >> type, for example:
  >>
  >> int[1] test;
  >> void[] v = test;
  >> printf("v.length = %d", v.length);
  >>
  >
  > snip
  >
  > From that code :
  >
  > test.length  would equal 1 and
  > v.length would equal 4;
  >
  > Do  you agree that this is inconsistent?
  > Is there a reason for this, or is it a bug(which I doubt)?
  >
  > Phill.


 Not really inconsistent. This gives the same output, just requires the
cast:
 int[1] test;
 byte[] v = cast(byte[])test;
 printf("v.length = %d", v.length);

 --
 Christopher E. Miller
Mar 05 2004
parent reply Ilya Minkov <minkov cs.tum.edu> writes:
byte[].length returns a count of elements an array can call, just as 
with int[]. It's just that with byte [], the basic type happens to be a 
byte, thus the physical length of an array is length. :> Int has a basic 
type width of 4 bytes on 32-bit platforms.

For void[], it just would not make sense to have anything different than 
the length in bytes. void[] is a memory block of specific length, but 
unspecific type. How imagine .length was not adjusted like it is now - 
you could never know how long this block of memory really is, since you 
cannot find out what type the array originally contained.

I don't find this inconsistent, since void* has always been just a 
pointer to (untyped) bytes, which follows from pointer arithmetic 
granularity.

-eye

Phill schrieb:

 That should have been:
 
 
  but the void[] and now I see the byte[] seem
  to be reffering to somthing completely different,
  when using ".length" on them. Which seems to
  be somthing similar to what ".size"  returns at the previous URL that I
  mentioned.
 
  ie:
 
 
  int[8] test;
  void[] v = test;
  byte[] b = cast(byte[])test;
  printf("test.length = %d", test.length); //test.length = 8
  printf("test.size = %d", test.size); // test.size = 32
  printf("v.length = %d", v.length); // v.length = 32
  printf("b.length = %d", b.length); //b.length = 32
  Can you correct me if I am wrong?
 
  Phill.
 
"Vathix" <vathix dprogramming.com> wrote in message
news:c2a3e1$1mqj$2 digitaldaemon.com...

Phill wrote:

 "Vathix" <vathix dprogramming.com> wrote in message
 news:c28cbo$1oom$1 digitaldaemon.com...

 Phill wrote:


 Ive got a (probably)dumb question here.

 What is a void? I thought it was nothing.
 It seems that in D it has a new meaning.

 For example, how do I call this method?

 aMethod(void[] buf)
 {
 }

 It seems to me that I should need a char[]

 I had a quick look at  the digtalsmars/d
 but couldnt see it, and URL's or quick
 tutorials via here, would be great :o))

 Thanks for any help.

 Phill.
It's actually a nice feature that allows you to have an array of any type, for example: int[1] test; void[] v = test; printf("v.length = %d", v.length);
snip From that code : test.length would equal 1 and v.length would equal 4; Do you agree that this is inconsistent? Is there a reason for this, or is it a bug(which I doubt)? Phill.
Not really inconsistent. This gives the same output, just requires the
cast:
int[1] test;
byte[] v = cast(byte[])test;
printf("v.length = %d", v.length);

--
Christopher E. Miller
Mar 05 2004
parent reply "Phill" <phill pacific.net.au> writes:
"Ilya Minkov" <minkov cs.tum.edu> wrote in message
news:c2b5pa$gmn$1 digitaldaemon.com...
 byte[].length returns a count of elements an array can call, just as
 with int[]. It's just that with byte [], the basic type happens to be a
 byte, thus the physical length of an array is length. :> Int has a basic
 type width of 4 bytes on 32-bit platforms.
Then the byte[].length is like calling int[].size, isnt it? As int[].length refers to the amount of indexes, where byte[].length doesnt. int[].length seems to follow this table, whereas byte[].length doesnt: ---------------------------- Array Properties Static array properties are: size Returns the array length multiplied by the number of bytes per array element. length Returns the number of elements in the array. This is a fixed quantity for static arrays. dup Create a dynamic array of the same size and copy the contents of the array into it. reverse Reverses in place the order of the elements in the array. Returns the array. sort Sorts in place the order of the elements in the array. Returns the array. Dynamic array properties are: size Returns the size of the dynamic array reference, which is 8 on 32 bit machines. length Get/set number of elements in the array. dup Create a dynamic array of the same size and copy the contents of the array into it. reverse Reverses in place the order of the elements in the array. Returns the array. sort Sorts in place the order of the elements in the array. Returns the array. Forgive me if I seem to be thick :o)) Thanks Phill.
Mar 05 2004
parent reply Ilya Minkov <minkov cs.tum.edu> writes:
Phill schrieb:

 "Ilya Minkov" <minkov cs.tum.edu> wrote in message
 news:c2b5pa$gmn$1 digitaldaemon.com...
 
byte[].length returns a count of elements an array can call, just as
with int[]. It's just that with byte [], the basic type happens to be a
byte, thus the physical length of an array is length. :> Int has a basic
type width of 4 bytes on 32-bit platforms.
Then the byte[].length is like calling int[].size, isnt it? As int[].length refers to the amount of indexes, where byte[].length doesnt.
It does, even if you cannot index into the array with a [] operator. In the sense of pointer arithmetic, void* indexing has always been defined the same as byte*. You must consider that you cannot use this void[] as typed data, since it is, by definition, anything. This is only to be used to transfer untyped data, or reusable memory buffers. In both cases, you would actually cast it before using it.
 int[].length seems to follow this table, whereas
 byte[].length doesnt:
 ----------------------------
I'm sorry, if you have an array of bytes, it can store exactly .length number of elements. Now that coincides with its physical size in memory, but that's all there is to it. :> And the reason for the coincidence is that .size is defined to be the size in memory in BYTES. :>
 Array Properties
--- 8< --- >8 ---
 Forgive me if I seem to be thick :o))
It's ok, i just don't get where you get stuck. :> -eye
Mar 06 2004
parent reply "Phill" <phill pacific.net.au> writes:
"Ilya Minkov" <minkov cs.tum.edu> wrote in message
news:c2d871$10m8$1 digitaldaemon.com...
 Phill schrieb:

 "Ilya Minkov" <minkov cs.tum.edu> wrote in message
 news:c2b5pa$gmn$1 digitaldaemon.com...

byte[].length returns a count of elements an array can call, just as
with int[]. It's just that with byte [], the basic type happens to be a
byte, thus the physical length of an array is length. :> Int has a basic
type width of 4 bytes on 32-bit platforms.
Then the byte[].length is like calling int[].size, isnt it? As int[].length refers to the amount of indexes, where byte[].length doesnt.
It does, even if you cannot index into the array with a [] operator. In the sense of pointer arithmetic, void* indexing has always been defined the same as byte*. You must consider that you cannot use this void[] as typed data, since it is, by definition, anything. This is only to be used to transfer untyped data, or reusable memory buffers. In both cases, you would actually cast it before using it.
 int[].length seems to follow this table, whereas
 byte[].length doesnt:
 ----------------------------
I'm sorry, if you have an array of bytes, it can store exactly .length number of elements. Now that coincides with its physical size in memory, but that's all there is to it. :> And the reason for the coincidence is that .size is defined to be the size in memory in BYTES. :>
 Array Properties
--- 8< --- >8 ---
 Forgive me if I seem to be thick :o))
It's ok, i just don't get where you get stuck. :>
I do understand everything that you say and have all along. I just think that .length is supposed to reffer to the amount of indexes in the array, if it does then why am I getting this error? error: lengths dont match for array copy when I run this code: int[10] aray; byte[10] bray = cast(byte[])aray; printf("aray.length =%d ", aray.length); printf("bray.length = %d ", bray.length); Is it because the size of the data in each index is larger? Phill
 -eye
Mar 06 2004
parent reply Ilya Minkov <minkov cs.tum.edu> writes:
When you cast one array type into another, the array data is not being 
converted, only the index is. Thus an int[10] casts into byte[40]. But 
these are not the same values, it's just "bit noise" from ints. 
Converting the whole array would be an expensive operation involving an 
allocation and a loop over all aray elements.

-eye

Phill schrieb:

 I do understand everything that you say and have all along.
 
 I just think  that .length is supposed to reffer to the amount of indexes in
 the array, if it does then why
 am I getting this error?
 error: lengths dont match for array copy
 when I run this code:
 
 int[10]  aray;
 byte[10] bray =  cast(byte[])aray;
 printf("aray.length =%d ", aray.length);
 printf("bray.length = %d ", bray.length);
 
 Is it because the size of the data in each index
 is larger?
 
 Phill
 
Mar 06 2004
parent "Phill" <phill pacific.net.au> writes:
got it :o))

Thanks for explaining.

Phill.



"Ilya Minkov" <minkov cs.tum.edu> wrote in message
news:c2dm8p$1o1u$1 digitaldaemon.com...
 When you cast one array type into another, the array data is not being
 converted, only the index is. Thus an int[10] casts into byte[40]. But
 these are not the same values, it's just "bit noise" from ints.
 Converting the whole array would be an expensive operation involving an
 allocation and a loop over all aray elements.

 -eye

 Phill schrieb:

 I do understand everything that you say and have all along.

 I just think  that .length is supposed to reffer to the amount of
indexes in
 the array, if it does then why
 am I getting this error?
 error: lengths dont match for array copy
 when I run this code:

 int[10]  aray;
 byte[10] bray =  cast(byte[])aray;
 printf("aray.length =%d ", aray.length);
 printf("bray.length = %d ", bray.length);

 Is it because the size of the data in each index
 is larger?

 Phill
Mar 06 2004
prev sibling parent reply "Walter" <walter digitalmars.com> writes:
"Phill" <phill pacific.net.au> wrote in message
news:c26sft$1r3m$1 digitaldaemon.com...
 Ive got a (probably)dumb question here.

 What is a void? I thought it was nothing.
 It seems that in D it has a new meaning.

 For example, how do I call this method?

 aMethod(void[] buf)
 {
 }

 It seems to me that I should need a char[]
'void*' in C is a pointer to data of unspecified type. Analogously, void[] in D is an array of data of unspecified type. The .length property is the number of bytes long it is. Could you use byte[] instead? Sure, but you'd prefer void[] for the same reasons that void* is used in C rather than char*. Also, any array can be implicitly converted to void[], just like any pointer can be implicitly converted to void*. void[] is particularly useful for describing, say, the contents of an arbitrary disk file or a data packet.
Mar 05 2004
parent reply "Phill" <phill pacific.net.au> writes:
"Walter" <walter digitalmars.com> wrote in message
news:c2aum9$414$1 digitaldaemon.com...
 "Phill" <phill pacific.net.au> wrote in message
 news:c26sft$1r3m$1 digitaldaemon.com...
 Ive got a (probably)dumb question here.

 What is a void? I thought it was nothing.
 It seems that in D it has a new meaning.

 For example, how do I call this method?

 aMethod(void[] buf)
 {
 }

 It seems to me that I should need a char[]
'void*' in C is a pointer to data of unspecified type. Analogously, void[] in D is an array of data of unspecified type. The .length property is the number of bytes long it is. Could you use byte[] instead? Sure, but you'd prefer void[] for the same reasons that void* is used in C rather than char*. Also, any array can be implicitly converted to void[], just like any
pointer
 can be implicitly converted to void*. void[] is particularly useful for
 describing, say, the contents of an arbitrary disk file or a data packet.
Ok, thanks for that(Christopher also explained) While fiddling with this, I have noticed this: int[8] somthing; somthing.length// = 8 byte[8] somthing; somthing.length// = 32 The same as you know would happen to a void[] Is there an easy way to get the amount of indexes in these(like there is for the int) to iterate through the array? The only way that I can see is to divide it by four, or catch the exception, so that you dont go out of bounds. Is there another way? eg: void printStuff(int code, void[] disguise) { if(code == 1) { char[] unmasked = cast(char[])disguise; printf(unmasked); } else { try { for(int i = 0; i < disguise.length ; i++) { int[] iArray = cast(int[])disguise; printf("%d", iArray[i]); } } catch (Object obj) { printf("argh!"); } } Alternatively I could have used i < (disguise.length / 4); Is there a better way? Phill.
Mar 05 2004
parent J Anderson <REMOVEanderson badmama.com.au> writes:
Phill wrote:

Alternatively I could have used
i < (disguise.length / 4);

Is there a better way?

Phill.

  
Instead of / 4 use/ type.sizeof and then you can put the entire thing in a template. -- -Anderson: http://badmama.com.au/~anderson/
Mar 05 2004