Page 1 of 1

Why #pragma pack(1)???

Posted: Tue Dec 27, 2022 6:28 pm
by chi
Why does PB align structures to a 1 byte boundary by default? There must be a good reason for that, right?!

Re: Why #pragma pack(1)???

Posted: Tue Dec 27, 2022 7:06 pm
by PeDe
Maybe for compatibility with Visual Basic <= 5. Only later VB6 used the C standard with user types.

Peter

Re: Why #pragma pack(1)???

Posted: Tue Dec 27, 2022 8:05 pm
by chi
PeDe wrote: Tue Dec 27, 2022 7:06 pm Maybe for compatibility with Visual Basic <= 5. Only later VB6 used the C standard with user types.
Ok, maybe. But this downward "compatibility" forces me to add Align #PB_Structure_AlignC to every structure I create, just (in case) to get proper padding. Internal structures are already padded by PB (PB_Alignment) so adding pack(1) makes sense, but my own structures shouldn't be packed by default...

Re: Why #pragma pack(1)???

Posted: Tue Dec 27, 2022 8:11 pm
by Caronte3D
chi wrote: Tue Dec 27, 2022 8:05 pm ...forces me to add Align #PB_Structure_AlignC to every structure I create...
I'm a neofitos on this matter :? Which is the purpose of doing that?

Re: Why #pragma pack(1)???

Posted: Tue Dec 27, 2022 8:12 pm
by mk-soft
I hope it stays that way. I use a lot of structures for communication. I also use overlays of structures a lot.
They have to be packed 1 like this.

Re: Why #pragma pack(1)???

Posted: Tue Dec 27, 2022 9:04 pm
by chi
Caronte3D wrote: Tue Dec 27, 2022 8:11 pm
chi wrote: Tue Dec 27, 2022 8:05 pm ...forces me to add Align #PB_Structure_AlignC to every structure I create...
I'm a neofitos on this matter :? Which is the purpose of doing that?
Aligned memory access is faster than unaligned...
mk-soft wrote: Tue Dec 27, 2022 8:12 pm I hope it stays that way. I use a lot of structures for communication. I also use overlays of structures a lot.
They have to be packed 1 like this.
I know there are situations where structure packing is useful (e.g. sending data over tcp/udp), but most of the time I would prefer performance over size.

Re: Why #pragma pack(1)???

Posted: Tue Dec 27, 2022 10:27 pm
by STARGĂ…TE
chi wrote: Tue Dec 27, 2022 9:04 pm
Caronte3D wrote: Tue Dec 27, 2022 8:11 pm
chi wrote: Tue Dec 27, 2022 8:05 pm ...forces me to add Align #PB_Structure_AlignC to every structure I create...
I'm a neofitos on this matter :? Which is the purpose of doing that?
Aligned memory access is faster than unaligned...
All structure and variable memories are aligned in Pure Basic. Only the fields itself become unaligned when non-processor-word fields are included. However, if you prefer performance over size, you would anyway use always integers and doubles instead of bytes, words, longs, etc.