Search code examples
c++cross-platformlong-integerendiannessunions

Does Endian-ness affect the union members when they are integers?


union Chunk
{
  struct { uint32_t index, total; } m_;
  uint64_t m_PlaceHolder;
} chunk;
chunk.m_.index = 1;
chunk.m_.total = 2;
SendOverTCPNetwork(chunk.m_PlaceHolder); // different platform OS will receive this

A union member is set for 2 integers and then a (combined) long integer is sent over a TCP network as shown above in the pseudo code.

Question: Will the endian-ness of the source machine & destination machine affect the values of the chunk variable?

In other words, will we be receiving the same value on the other side?


Solution

  • Will the endian-ness of the source machine & destination machine affect the values of the chunk variable?

    Yes. Endianness affects all integers, even when they are members of classes. (Except of course signed char and unsigned char).


    SendOverTCPNetwork(m_PlaceHolder);
    

    You cannot access non-static members without an object. The example program is ill-formed.