Is there any way to specifically create a 2, 4, (or some other arbitrary number) byte integer instead of using a short int or long int and hoping it will take up the correct number of bytes?
The reason I ask is that I am working on a game, in which the board will be made up of tiles. These tiles will each have a specific type to keep track of whether that certain tile is a grass tile, a water tile, etc. I'll probably have more than 255 different types of tiles so I can't get away with using an unsigned char. On a small board of 1000 * 1000, the difference between using two bytes to store each type and using four bytes to store each type is 2 MB of wasted space (which would happen if my compiler decided to use four bytes for a short int). Although not a huge problem, still not great.
Most of the time it doesn't matter if a short int takes up 2 or 4 bytes. The exception is when you allocate large (multidimensional) arrays of them.
"uint8", "uint16", "uint32", & "uint64" come to mind (I can't remember where I saw them though), but my compiler complains that they aren't declared when I try to use them.
I suppose one way might be to create a class that internally handles unsigned char arrays and overload it's operators, but I was hoping there would be a better solution.



LinkBack URL
About LinkBacks




CornedBee