Best Practice for Numeric Types in Structures?

Just starting out? Need help? Post your questions and find answers here.
swhite
Enthusiast
Enthusiast
Posts: 796
Joined: Thu May 21, 2009 6:56 pm

Best Practice for Numeric Types in Structures?

Post by swhite »

Hi

I was wondering whether it is better to use integer types in structures as opposed to ascii, byte or word etc. The reason I ask is I have a number of items in the structure that will either be 1 or 0 and others that will never be more than 255. So I was thinking about using the smaller types and wondered if this is good practice or whether I should just stick with basic integer types.

Thanks
Simon
Simon White
dCipher Computing
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Best Practice for Numeric Types in Structures?

Post by ts-soft »

You can use byte and others, that's make sense in a structure. You save memory, which is rarely given to a normal variable.
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Re: Best Practice for Numeric Types in Structures?

Post by Lunasole »

Well
- it was preferred to use integers (to align structures by dword bounds and get some speed enhancement), nowadays that enhancement is not noticeable, also you can anyway align short types using that Align keyword
- while using smaller types allows to save memory. but also it doesn't matter if you save whole 3 bytes on PC with 16GB RAM, even if 3 * 1kk or much more

I'm using limited types just because it is more usable to have shorter variable if you know it will never be > 255, or 128, 65535.
On other side, you really always need to know can it be > or not, else you will get sometime extra pain caused by type overflow

// talking too much about nothing before goto sleep ^^
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: Best Practice for Numeric Types in Structures?

Post by Shield »

If you are defining a structure that is to be used as part of an array,
always use the smallest possible type that can fit your data.

Lunasole is correct in that it doesn't matter too much for RAM, however,
it does for cache locality. Having a tightly packed structure allows the CPU
to fit more elements into cache which makes processing it magnitudes faster.
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
swhite
Enthusiast
Enthusiast
Posts: 796
Joined: Thu May 21, 2009 6:56 pm

Re: Best Practice for Numeric Types in Structures?

Post by swhite »

Thank-you all for your replies. I think I will use the small values especially for items that are either 1 or zero (#true or #False).

Simon
Simon White
dCipher Computing
Post Reply