March 05, 2004 Re: void no type | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vathix | 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 |
March 05, 2004 Re: void no type | ||||
---|---|---|---|---|
| ||||
Posted in reply to Phill | 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
>
>
|
March 05, 2004 Re: void no type | ||||
---|---|---|---|---|
| ||||
Posted in reply to Phill | "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. |
March 06, 2004 Re: void no type | ||||
---|---|---|---|---|
| ||||
Posted in reply to Phill | 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
>>
>>
>
>
|
March 06, 2004 Re: void no type | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | "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. |
March 06, 2004 Re: void no type | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ilya Minkov | "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. |
March 06, 2004 Re: void no type | ||||
---|---|---|---|---|
| ||||
Posted in reply to Phill | 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/ |
March 06, 2004 Re: void no type | ||||
---|---|---|---|---|
| ||||
Posted in reply to Phill | 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 |
March 06, 2004 Re: void no type | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ilya Minkov | "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 |
March 06, 2004 Re: void no type | ||||
---|---|---|---|---|
| ||||
Posted in reply to Phill | 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
>
|
Copyright © 1999-2021 by the D Language Foundation