When does Endianness become a factor?

前端 未结 8 1932
天涯浪人
天涯浪人 2020-12-12 14:52

Endianness from what I understand, is when the bytes that compose a multibyte word differ in their order, at least in the most typical case. So that an 16-bit integer may b

8条回答
  •  时光说笑
    2020-12-12 15:14

    Here are some guidelines for C/C++ endian-neutral code. Obviously these are written as "rules to avoid"... so if code has these "features" it could be prone to endian-related bugs !! (this is from my article on Endianness published in Dr Dobbs)

    1. Avoid using unions which combine different multi-byte datatypes. (the layout of the unions may have different endian-related orders)

    2. Avoid accessing byte arrays outside of the byte datatype. (the order of the byte array has an endian-related order)

    3. Avoid using bit-fields and byte-masks (since the layout of the storage is dependent upon endianness, the masking of the bytes and selection of the bit fields is endian sensitive)

    4. Avoid casting pointers from multi-byte type to other byte types.
      (when a pointer is cast from one type to another, the endianness of the source (ie. The original target) is lost and subsequent processing may be incorrect)

提交回复
热议问题