Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
March 29, 2004 varargs with void[] | ||||
---|---|---|---|---|
| ||||
I remember vaguely Walter mentioning the use of void[] in D to facilitate the same ability as void* in C... could a little syntax sugar be devised to support variable parameter lists using void[]? Maybe something like: void myprintf(char[] format, void[][] @data) { int idx; char fc; char[] out; // ... switch (fc) { case 'd': out ~= .toString(cast(int) data[idx++]); break; // ... case 's': out ~= .toString(cast(char[]) data[idx++]); break; // ... } // ... } Just a thought. -C. Sauls -Invironz |
March 29, 2004 Re: varargs with void[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to C. Sauls Attachments: | Just to see if this would technically work at all, I tossed together this little experiment, with (IMHO) pretty good results. Please excuse the extrememly rudimentary print() function. :) [Source code attached] -C. Sauls -Invironz |
March 29, 2004 Re: varargs with void[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to C. Sauls | On Sun, 28 Mar 2004 20:24:25 -0600, "C. Sauls" <ibisbasenji@yahoo.com> wrote: >Just to see if this would technically work at all, I tossed together this little experiment, with (IMHO) pretty good results. Please excuse the extrememly rudimentary print() function. :) I think that code will only work for data that fits inside (void[]).sizeof bytes. Since "int" and "char[]" fit it works but with a big struct it wouldn't. For anything bigger it would be taking a (void[]).sizeof chunk out of the data and passing that around. Remember dynamic arrays consist of a length int followed by a pointer to the data (in this case that pointer has type void*). >[Source code attached] > >-C. Sauls >-Invironz |
March 29, 2004 Re: varargs with void[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Hinkle | Ben Hinkle wrote: >On Sun, 28 Mar 2004 20:24:25 -0600, "C. Sauls" <ibisbasenji@yahoo.com> >wrote: > > > >>Just to see if this would technically work at all, I tossed together this little experiment, with (IMHO) pretty good results. Please excuse the extrememly rudimentary print() function. :) >> >> > >I think that code will only work for data that fits inside >(void[]).sizeof bytes. Since "int" and "char[]" fit it works >but with a big struct it wouldn't. For anything bigger it >would be taking a (void[]).sizeof chunk out of the data and >passing that around. Remember dynamic arrays consist of a length int followed by a pointer to the data (in this case >that pointer has type void*). > > Why not simply make these things pointers? -- -Anderson: http://badmama.com.au/~anderson/ |
March 29, 2004 Re: varargs with void[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Hinkle | Just tried rigging up a struct with two int fields and a char[] field. The ints work jut fine, the char[] is lost, so you have a point.
-C. Sauls
-Invironz
Ben Hinkle wrote:
> On Sun, 28 Mar 2004 20:24:25 -0600, "C. Sauls" <ibisbasenji@yahoo.com>
> wrote:
>
>
>>Just to see if this would technically work at all, I tossed together this little experiment, with (IMHO) pretty good results. Please excuse the extrememly rudimentary print() function. :)
>
>
> I think that code will only work for data that fits inside
> (void[]).sizeof bytes. Since "int" and "char[]" fit it works
> but with a big struct it wouldn't. For anything bigger it
> would be taking a (void[]).sizeof chunk out of the data and
> passing that around. Remember dynamic arrays consist of a length int followed by a pointer to the data (in this case
> that pointer has type void*).
>
>
>>[Source code attached]
>>
>>-C. Sauls
>>-Invironz
>
>
|
March 29, 2004 Re: varargs with void[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to J Anderson | If you mean to make data of type void*[] instead of void[][] that won't work... I tried it. Cannot (apparently) cast a char[] or a struct to void*, but can cast anything to void[] (with varying degrees of success).
-C. Sauls
-Invironz
J Anderson wrote:
> Ben Hinkle wrote:
>
>> On Sun, 28 Mar 2004 20:24:25 -0600, "C. Sauls" <ibisbasenji@yahoo.com>
>> wrote:
>>
>>
>>
>>> Just to see if this would technically work at all, I tossed together this little experiment, with (IMHO) pretty good results. Please excuse the extrememly rudimentary print() function. :)
>>>
>>
>>
>> I think that code will only work for data that fits inside
>> (void[]).sizeof bytes. Since "int" and "char[]" fit it works
>> but with a big struct it wouldn't. For anything bigger it
>> would be taking a (void[]).sizeof chunk out of the data and
>> passing that around. Remember dynamic arrays consist of a length int followed by a pointer to the data (in this case
>> that pointer has type void*).
>>
>>
> Why not simply make these things pointers?
>
|
March 29, 2004 Re: varargs with void[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to C. Sauls | C. Sauls wrote: > If you mean to make data of type void*[] instead of void[][] that won't work... I tried it. Cannot (apparently) cast a char[] or a struct to void*, but can cast anything to void[] (with varying degrees of success). > > -C. Sauls > -Invironz What about? char [] string; void *tt = (void*)&string[0]; //Looses length void *t = (void*)string; struct a { } a A; void *t = (void*)&A; What do you mean? > > J Anderson wrote: > >> Ben Hinkle wrote: >> >>> On Sun, 28 Mar 2004 20:24:25 -0600, "C. Sauls" <ibisbasenji@yahoo.com> >>> wrote: >>> >>> >>> >>>> Just to see if this would technically work at all, I tossed together this little experiment, with (IMHO) pretty good results. Please excuse the extrememly rudimentary print() function. :) >>>> >>> >>> >>> >>> I think that code will only work for data that fits inside >>> (void[]).sizeof bytes. Since "int" and "char[]" fit it works >>> but with a big struct it wouldn't. For anything bigger it >>> would be taking a (void[]).sizeof chunk out of the data and >>> passing that around. Remember dynamic arrays consist of a length int followed by a pointer to the data (in this case >>> that pointer has type void*). >>> >>> >> Why not simply make these things pointers? >> -- -Anderson: http://badmama.com.au/~anderson/ |
Copyright © 1999-2021 by the D Language Foundation