1

I have two processes running on the same device (no VMs involved), communicating over a binary IPC protocol. Since this guarantees that the the representation of the number is the same for the sender and the receiver, can I safely assume that the following serialization will work on any device that supports floating point numbers?

void store_double(uint8_t *buf, double d)
{
    memcpy(buf, &d, sizeof(double));
}

double load_double(uint8_t const *buf)
{
    double d;
    memcpy(&d, buf, sizeof(double));
    return d;
}

double orig = 123.456;
uint8_t serialized[sizeof(double)];
store_double(serialized, orig);
// send serialized bytes to the receiver

// receive serialized bytes from the sender
double copy = load_double(serialized);
Bob Builder
  • 393
  • 2
  • 6
  • 1
    As your restrict your environment to avoid endianness issues, I would say it is safe (as long as `buf` is long enough, which is your case). – Jarod42 May 09 '15 at 17:47
  • 1
    Endian issues shouldn't matter if the hardware on both ends represents `double` values in IEEE double format: http://en.wikipedia.org/wiki/Double-precision_floating-point_format – Andrew Henle May 09 '15 at 18:48

1 Answers1

1

Since the sender and the receiver are identical architectures, there are no endian issues, floating point, integer, or otherwise.

If the architectures are different, you might have problems. See this post or this post.

Community
  • 1
  • 1
Craig S. Anderson
  • 6,966
  • 4
  • 33
  • 46