September 12, 2004
"mcl" wrote:
> I'm using the socket library in phobos and I
> want to send an aligned struct over the network.

A word of warning... When sending structured data over the network, keep in mind the endianness of the client and server may (eventually) be different.  For example, *if* there were a D compiler for non-x86-based platforms, such as the Mac, your struct could be sent from a Big-endian machine to a little-endian machine.  This would FUBAR any float/double (and multibyte integer, I think) data you may have in your struct, since machines of different endianness interpret multibyte datatypes differently.


September 12, 2004
In article <opsd298iol5a2sq9@digitalmars.com>,
 Regan Heath <regan@netwin.co.nz> wrote:

> It's a little known fact that you can slice a pointer giving you an array. Slicing a void* will give you a void[]. You can also cast any memory address to a void*, So...
> 
> template toVoid(Struct)
> {
>    void[] toVoid(inout Struct s)
>    {
>      return (cast(void*)&s)[0..s.sizeof];
>    }
> }
> 
> should do it.
> 
> Regan

That reeks of nasty hack.   Why slice a pointer to cast a struct into a void[] ???  Why can't it just be cast directly?
September 12, 2004
On Sat, 11 Sep 2004 12:56:07 +0000 (UTC), Nick <Nick_member@pathlink.com> wrote:
> In article <cht6ve$1vma$1@digitaldaemon.com>, Russ Lewis says...
>>
>> void[] fv = (&f)[0..f.sizeof].dup;
>>
>
> By the way, it's (&f)[0..1], not (&f)[0..f.sizeof]. If f is a Foo then (&f) is of type (Foo*), so a slice of one element will include one entire struct. I have corrected this in the wiki as well.

Ahhh, yes, but in this case...

(cast(void*)&f)[0..f.sizeof]

it is f.sizeof, isn't it? because void has a size of 1.

Regan

-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
September 12, 2004
On Sun, 12 Sep 2004 07:23:08 -0700, Sha Chancellor <schancel@pacific.net> wrote:
> In article <opsd298iol5a2sq9@digitalmars.com>,
>  Regan Heath <regan@netwin.co.nz> wrote:
>
>> It's a little known fact that you can slice a pointer giving you an array.
>> Slicing a void* will give you a void[]. You can also cast any memory
>> address to a void*, So...
>>
>> template toVoid(Struct)
>> {
>>    void[] toVoid(inout Struct s)
>>    {
>>      return (cast(void*)&s)[0..s.sizeof];
>>    }
>> }
>>
>> should do it.
>>
>> Regan
>
> That reeks of nasty hack.

Not to me, but then I work in C most of the time.

> Why slice a pointer to cast a struct into a
> void[] ???  Why can't it just be cast directly?

Good question, it probably could with some compiler magic, after all a void[] is simply a pointer and a length, the compiler knows the length of the object..

Arrays already implicitly cast to void[] eg.

void foo(void[] p)
{
}

void main()
{
  char[] string = "regan";
  foo(string);
}

Regan

-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
September 13, 2004
Regan Heath wrote:

> On Sat, 11 Sep 2004 12:56:07 +0000 (UTC), Nick <Nick_member@pathlink.com> wrote:
> 
>> In article <cht6ve$1vma$1@digitaldaemon.com>, Russ Lewis says...
>>
>>>
>>> void[] fv = (&f)[0..f.sizeof].dup;
>>>
>>
>> By the way, it's (&f)[0..1], not (&f)[0..f.sizeof]. If f is a Foo then (&f) is of type (Foo*), so a slice of one element will include one entire struct. I have corrected this in the wiki as well.
> 
> 
> Ahhh, yes, but in this case...
> 
> (cast(void*)&f)[0..f.sizeof]
> 
> it is f.sizeof, isn't it? because void has a size of 1.

But Nick isn't casting to void so it's a pointer to Foo.


Sean
September 13, 2004
In article <opsd8ixnk45a2sq9@digitalmars.com>, Regan Heath says...
> Nick:
>> By the way, it's (&f)[0..1], not (&f)[0..f.sizeof]. If f is a Foo then (&f) is of type (Foo*), so a slice of one element will include one entire struct. I have corrected this in the wiki as well.
>
>Ahhh, yes, but in this case...
>
>(cast(void*)&f)[0..f.sizeof]
>
>it is f.sizeof, isn't it? because void has a size of 1.

That is correct.

Nick


September 13, 2004
On Sun, 12 Sep 2004 22:08:05 -0700, Sean Kelly <sean@f4.ca> wrote:
> Regan Heath wrote:
>
>> On Sat, 11 Sep 2004 12:56:07 +0000 (UTC), Nick <Nick_member@pathlink.com> wrote:
>>
>>> In article <cht6ve$1vma$1@digitaldaemon.com>, Russ Lewis says...
>>>
>>>>
>>>> void[] fv = (&f)[0..f.sizeof].dup;
>>>>
>>>
>>> By the way, it's (&f)[0..1], not (&f)[0..f.sizeof]. If f is a Foo then (&f) is of type (Foo*), so a slice of one element will include one entire struct. I have corrected this in the wiki as well.
>>
>>
>> Ahhh, yes, but in this case...
>>
>> (cast(void*)&f)[0..f.sizeof]
>>
>> it is f.sizeof, isn't it? because void has a size of 1.
>
> But Nick isn't casting to void so it's a pointer to Foo.

I know.

I was simply pointing out the original suggestion wasn't buggy, it turned buggy when someone changed a part of it. :)

Regan

-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
September 25, 2004
In article <opsd8ly80b5a2sq9@digitalmars.com>,
 Regan Heath <regan@netwin.co.nz> wrote:

> On Sun, 12 Sep 2004 07:23:08 -0700, Sha Chancellor <schancel@pacific.net> wrote:
> > In article <opsd298iol5a2sq9@digitalmars.com>,
> >  Regan Heath <regan@netwin.co.nz> wrote:
> >
> >> It's a little known fact that you can slice a pointer giving you an
> >> array.
> >> Slicing a void* will give you a void[]. You can also cast any memory
> >> address to a void*, So...
> >>
> >> template toVoid(Struct)
> >> {
> >>    void[] toVoid(inout Struct s)
> >>    {
> >>      return (cast(void*)&s)[0..s.sizeof];
> >>    }
> >> }
> >>
> >> should do it.
> >>
> >> Regan
> >
> > That reeks of nasty hack.
> 
> Not to me, but then I work in C most of the time.
> 
> > Why slice a pointer to cast a struct into a
> > void[] ???  Why can't it just be cast directly?
> 
> Good question, it probably could with some compiler magic, after all a void[] is simply a pointer and a length, the compiler knows the length of the object..
> 
> Arrays already implicitly cast to void[] eg.
> 
> void foo(void[] p)
> {
> }
> 
> void main()
> {
>    char[] string = "regan";
>    foo(string);
> }
> 
> Regan

I guess this bothers me:

> >>    void[] toVoid(inout Struct s)
> >>    {
> >>      return (cast(void*)&s)[0..s.sizeof];
> >>    }

You cast a pointer of S to a void* and then do an array slice.   If you can slice it like an array, why do you need to slice it at all? Why can't it be implicit cast to void[] from a void* in the first place? Why is slicing necessary?

IE:

return (cast(void[])&s);
or return (cast(void*)&s);

Will either of those work?

I don't have a D compiler handy.
September 26, 2004
"Sha Chancellor" <schancel@pacific.net> wrote in message news:schancel-0064DE.14485525092004@digitalmars.com...
> In article <opsd8ly80b5a2sq9@digitalmars.com>,
>  Regan Heath <regan@netwin.co.nz> wrote:
>
> > On Sun, 12 Sep 2004 07:23:08 -0700, Sha Chancellor
<schancel@pacific.net>
> > wrote:
> > > In article <opsd298iol5a2sq9@digitalmars.com>,
> > >  Regan Heath <regan@netwin.co.nz> wrote:
> > >
> > >> It's a little known fact that you can slice a pointer giving you an
> > >> array.
> > >> Slicing a void* will give you a void[]. You can also cast any memory
> > >> address to a void*, So...
> > >>
> > >> template toVoid(Struct)
> > >> {
> > >>    void[] toVoid(inout Struct s)
> > >>    {
> > >>      return (cast(void*)&s)[0..s.sizeof];
> > >>    }
> > >> }
> > >>
> > >> should do it.
> > >>
> > >> Regan
> > >
> > > That reeks of nasty hack.
> >
> > Not to me, but then I work in C most of the time.
> >
> > > Why slice a pointer to cast a struct into a
> > > void[] ???  Why can't it just be cast directly?
> >
> > Good question, it probably could with some compiler magic, after all a void[] is simply a pointer and a length, the compiler knows the length
of
> > the object..
> >
> > Arrays already implicitly cast to void[] eg.
> >
> > void foo(void[] p)
> > {
> > }
> >
> > void main()
> > {
> >    char[] string = "regan";
> >    foo(string);
> > }
> >
> > Regan
>
> I guess this bothers me:
>
> > >>    void[] toVoid(inout Struct s)
> > >>    {
> > >>      return (cast(void*)&s)[0..s.sizeof];
> > >>    }
>
> You cast a pointer of S to a void* and then do an array slice.   If you can slice it like an array, why do you need to slice it at all? Why can't it be implicit cast to void[] from a void* in the first place? Why is slicing necessary?

Because void[] has a length property and a void* doesn't,
if we could implicitly cast void* to void[] what length would that
array have? That is what slicing is there for, to give array length,
it is also a documented way of making bounds checked array out of
a pointer.

> IE:
>
> return (cast(void[])&s);
> or return (cast(void*)&s);
>
> Will either of those work?

No, first is Struct* to void[]
the second is void* to return type void[]

>
> I don't have a D compiler handy.


September 26, 2004
In article <cj5qg7$2uod$1@digitaldaemon.com>,
 "Ivan Senji" <ivan.senji@public.srce.hr> wrote:

> Because void[] has a length property and a void* doesn't,
> if we could implicitly cast void* to void[] what length would that
> array have? That is what slicing is there for, to give array length,
> it is also a documented way of making bounds checked array out of
> a pointer.
> 
> > IE:
> >
> > return (cast(void[])&s);
> > or return (cast(void*)&s);
> >
> > Will either of those work?
> 
> No, first is Struct* to void[]
> the second is void* to return type void[]

Hmmm..  That make sense now. But, I still don't like how it looks.
1 2
Next ›   Last »