I'm working on a project to convert an ancient Solaris (sparc) targeted application to Linux. The language is C/C++, and most of the code was written by electronics engineers -- that is, people who do a lot of embedded device work.
As such, unions and bitfields are used all over the place. It's pretty straightforward to deal with structs & whatnot that don't involve bitfields or unions, but because of the convention for which bit is interpreted as the MSB or LSB, as well as byte ordering, it can be rather irritating to convert code over to x86 linux.
I'm looking for a clean meth开发者_运维百科odology for bringing this old code into the 21st century. Any suggestions (examples, research papers, whitepapers, book suggestions, etc) are quite welcome.
Are those structs used for saving data as files or sending it over some sort of serial/network link? You only need to worry about endianness when the data is going to move off of the machine to another machine.
It's a horribly bad practice to use bitfields in structures used in something other than in-memory data. You might be better off keeping the structures as-is throughout the program, and just modify the functions that read/write them. At that point, you can do the byte order conversions cleanly.
精彩评论