![]() |
Struct padding - Printable Version +- Tilengine - The 2D retro graphics engine forum (http://tilengine.org/forum) +-- Forum: English forums (http://tilengine.org/forum/forumdisplay.php?fid=3) +--- Forum: Support (http://tilengine.org/forum/forumdisplay.php?fid=7) +--- Thread: Struct padding (/showthread.php?tid=2308) |
Struct padding - System64 - 02-06-2025 Hi! I have a question. Are structs automatically padded during compilation? I recommend you to order your fields by size or manually padding your fields like this : Field ordering : Code: typedef struct Manual padding : Code: typedef struct RE: Struct padding - megamarc - 02-07-2025 Hi! When arranging structs in C, to maximize packing it's recommended to group members by type size, starting from the biggest one down to the smallest one. On the contrary, mixing types leads to holes in memory, as the compiler puts each member on the nearest memory address that is a multiple of the member's type size. That's why ordering members by their type, from bigger to smallest, plays nice with memory boundaries. However, I guess I decided to arrange members based on their relevance. So the id and gid members are "key fields", whereas x and y are lesser attributes. This helps when debugging because important fields appear first, albeit some tens of bytes can be wasted. Thanks for making this observation :-) RE: Struct padding - System64 - 02-07-2025 (02-07-2025, 03:27 AM)megamarc Wrote: Hi! You're welcome! I noticed this issue while making a binding. I don't know if it's the same behavior between ARM and x86_64 either. Quite a sensitive topic. |