|
|
|
|
|
by filam
2791 days ago
|
|
I have to admit I stopped reading after the combined section on byte swapping and casting external data to a internal structure. Perhaps the author of the code should have used ntohs() instead of be16toh(), but the effect is the same? There aren't any conditionals in ntoh... https://commandcenter.blogspot.com/2012/04/byte-order-fallac... And will a compiler make use of native byte swapping instructions with the code proposed by the author? |
|
I think this is also the main point of the post you linked: the "bad" example has a numeric value and then performs byte swapping on it, the "good" examples only ever construct "correctly ordered" numeric values.
If you can possibly get to a place where you have a uint16 but the value is "in the wrong byte order", you've done something wrong already.
Admitting the possibility of values with the wrong byte order into your system casts every single value into doubt. Of course it's possible to resolve that doubt on a case-by-case basis by carefully identifying the right places to insert ntohs()/htons() calls, but to me that sounds like creating extra chores for yourself while working against the language.