Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><em>Apart from that, is there any other drawback that I miss?</em></p> <p>I'm afraid you've misunderstood the nature of endian conversion problems. "Big endian" doesn't mean your fields are laid out in reverse, so that a </p> <pre><code>struct msg1_bigendian { int32 a; int16 b; uint32 c } </code></pre> <p>on a big endian architecture is equivalent to a</p> <pre><code>struct msg1_littleendian { uint32 c; int16 b; int32 a; } </code></pre> <p>on a little endian architecture. Rather, it means that the byte-order <em>within</em> each field is reversed. Let's assume:</p> <pre><code>a = 0x1000000a; b = 0xb; c = 0xc; </code></pre> <p>On a big-endian architecture, this will be laid out as:</p> <pre><code>10 00 00 0a 00 0b 00 00 00 0c </code></pre> <p>The high-order (most significant) byte comes first.</p> <p>On a little-endian machine, this will be laid out as:</p> <pre><code>0a 00 00 10 0b 00 0c 00 00 00 </code></pre> <p>The lowest order byte comes first, the highest order last.</p> <p>Serialize them and overlay the serialized form of the messages on top of each other, and you will discover the incompatibility:</p> <pre><code>10 00 00 0a 00 0b 00 00 00 0c (big endian) 0a 00 00 10 0b 00 0c 00 00 00 (little endian) int32 a int16 b int32 c </code></pre> <p>Note that this isn't simply a case of the fields running in reverse. You proposal would result in a little endian machine mistaking the big endian representation as:</p> <p>a = 0xc000000; b = 0xb00; c = 0xa000010;</p> <p>Certainly not what was transmitted!</p> <p>You really do have to convert every individual field to network byte order and back again, for every field transmitted. </p> <p>UPDATE:</p> <p>Ok, I understand what you are trying to do now. You want to define the struct in reverse, then memcpy from the <em>end</em> of the byte string to the beginning (reverse copy) and reverse the byte order that way. In which case I would say, yes, this is a hack, and yes, it makes your code un-portable, and yes, it isn't worth it. Converting between byte orders is not, in fact, a very expensive operation and it is far easier to deal with than reversing the layout of every structure. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload