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
Best Practice for Numeric Types in Structures?
Best Practice for Numeric Types in Structures?
Simon White
dCipher Computing
dCipher Computing
Re: Best Practice for Numeric Types in Structures?
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.

Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.

Re: Best Practice for Numeric Types in Structures?
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 ^^
- 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"
Re: Best Practice for Numeric Types in Structures?
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.
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.
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
Re: Best Practice for Numeric Types in Structures?
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
Simon White
dCipher Computing
dCipher Computing