Subject: Re: Encoding of doulbes by xdr
To: None <current-users@NetBSD.ORG>
From: der Mouse <mouse@Collatz.McRCIM.McGill.EDU>
List: current-users
Date: 03/24/1995 14:11:06
[trouble with xdr and doubles]

>   char *xdr_buffer=NULL;
>   XDR xdrs;
>   int nbytes=sizeof(double);

>   xdr_buffer = (char*)malloc(nbytes);

>   xdrmem_create(&xdrs, xdr_buffer, nbytes, XDR_ENCODE);
>   xdr_double(&xdrs, &x);
>   xdr_destroy(&xdrs);

There's a bug here, though since it is latent on most current machines
it is extremely unlikely to be your problem.

The bug is that sizeof(double) is not necessarily the right number of
bytes for an XDR-encoded double.  The whole point of XDR is that the
encoding (viewed as an octet stream) is independent of the
architecture...and thus you should be allocating 8 bytes (I assume - I
don't know enough about XDR to be sure), not sizeof(double) bytes.

The reason I say it's extremely unlikely to be your problem is that on
most current machines, sizeof(double) does happen to be 8.

Actually, you want to allocate enough space to hold 8 generated octets.
Computing the value to pass to malloc gets more complicated if you want
to make it fully correct even on machines where char != octet.  You
have to know how the XDR library packs octets into chars to compute
this value; on a machine with 8-bit chars, you're probably safe in
assuming the trivial packing.  Mind you, I personally probably wouldn't
worry about this, though I would about the sizeof(double) point above.

					der Mouse