Originally Posted by
CodeSlapper
I have struct that I am populating, and I'm trying to figure out what is the best way to utilize them.
Some of the data types are floats, bools, and unsigned ints. The unsigned ints range from say 2 bits to 10 bits being used. Do people normally store each one in the smallest possible data type that holds the data, such as char, short, etc.?
I've seen other instances where bit fields are used to somewhat keep the size of the struct down. If you use int i:3, does that only allow values with 3 bits to be stored in the variable? I know this only works to some extent. If you have an OS that uses 4 byte ints, and you used 33 bit fields it would say the data in 2 ints.
The other method is to store the data in an int with bit shifting and bit masks. This is probably the best approach to keep memory usage down, and may lose some flexibility with the interface if the size of variables change.
I'd like to know what people normally do, and I think bit fields are pretty easy and convenient, but I could be way off.