Page 1 of 1
Best Practice for Numeric Types in Structures?
Posted: Fri Sep 15, 2017 9:54 pm
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
Re: Best Practice for Numeric Types in Structures?
Posted: Fri Sep 15, 2017 10:23 pm
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.
Re: Best Practice for Numeric Types in Structures?
Posted: Sat Sep 16, 2017 7:03 am
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 ^^
Re: Best Practice for Numeric Types in Structures?
Posted: Sat Sep 16, 2017 8:52 am
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.
Re: Best Practice for Numeric Types in Structures?
Posted: Sat Sep 16, 2017 2:27 pm
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