Reorder struct members to prevent padding
Posted: Wed Oct 11, 2023 3:27 pm
I've heard about structure padding to reduce R/W operations to access a struct member, and that there is "packed" option to disable this padding optimization.
I would like to write struct in certain order, to keep mutual parameters together.
But to apply reorder optimization at compilation to reduce used memory space while minimalizing R/W operations for most struct members.
Note: __reorder__ is made up, but I am looking for some attribute with this effect
I know that, according to C standard, compiler is not allowed to automatically reorder structure, but can I somehow explicitly ask it to do so for particular structure?
I would like to write struct in certain order, to keep mutual parameters together.
But to apply reorder optimization at compilation to reduce used memory space while minimalizing R/W operations for most struct members.
Code: Select all
struct
{
bool param1_enabled :1;
int param1_value1;
int param1_value2;
bool param2_enabled :1;
int param2_value1;
}
// size: 20
Code: Select all
struct __attribute__(__reorder__) // produces:
{
// ints
int param1_value1;
int param1_value2;
int param2_value1;
// bools
bool param2_enabled :1;
bool param1_enabled :1;
}
// size: 13
I know that, according to C standard, compiler is not allowed to automatically reorder structure, but can I somehow explicitly ask it to do so for particular structure?