Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to worry about endianness?

Tags:

c

endianness

I have seen countless references about endianness and what it means. I got no problems about that... However, my coding project is a simple game to run on linux and windows, on standard "gamer" hardware. Do I need to worry about endianness in this case? When should I need to worry about it? My code is simple C and SDL+GL, the only complex data are basic media files (png+wav+xm) and the game data is mostly strings, integer booleans (for flags and such) and static-sized arrays. So far no user has had issues, so I am wondering if adding checks is necessary (will be done later, but there are more urgent issues IMO).

like image 616
GAKOKO Avatar asked Feb 06 '10 14:02

GAKOKO


3 Answers

The times when you need to worry about endianess:

  • you are sending binary data between machines or processes (using a network or file). If the machines may have different byte order or the protocol used specifies a particular byte order (which it should), you'll need to deal with endianess.
  • you have code that access memory though pointers of different types (say you access a unsigned int variable through a char*).

If you do these things you're dealing with byte order whether you know it or not - it might be that you're dealing with it by assuming it's one way or the other, which may work fine as long as your code doesn't have to deal with a different platform.

In a similar vein, you generally need to deal with alignment issues in those same cases and for similar reasons. Once again, you might be dealing with it by doing nothing and having everything work fine because you don't have to cross platform boundaries (which may come back to bite you down the road if that does become a requirement).

like image 156
Michael Burr Avatar answered Oct 04 '22 16:10

Michael Burr


If you mean a PC by "standard gamer hardware", then you don't have to worry about endianness as it will always be little endian on x86/x64. But if you want to port the project to other architectures, then you should design it endianness-independently.

like image 24
AndiDog Avatar answered Oct 04 '22 14:10

AndiDog


Whenever you recieve/transmit data from a network, remeber to convert to/from network and host byte order. The C functions htons, htonl etc, or equivalients in your language, should be used here.

Whenever you read multi-byte values (like UTF-16 characters or 32 bit ints) from a file, since that file might have originated on a system with different endianness. If the file is UTF 16 or 32 it probably has a BOM (byte-order mark). Otherwise, the file format will have to specify endianness in some way.

like image 36
gnud Avatar answered Oct 04 '22 16:10

gnud