how to disable structure padding in qt uisng mingw
-
I have structure with with 2 members one with data
struct xxx
{
std::uint32_t x;
std::uint8_t y;
};Now the issue is even if variable y is declared as 8 bit integer it is also occupying 32 bit. What I understood is , it is because of structure padding to maintain memory alignment.
Issue got resolved after adding #pragma pack(push,1)Is there any way to disable structure padding using some qt creator or compiler settings ?
-
@jsulm Hi...thanks for the quick reply. But i just want a solution to handle this by compiler by its own other than me adding pragma manually.
If not qt, can we configure mingw compiler to handle it . Any alternative solution other than adding it in code.
@Parvathy2020 said in how to disable structure padding in qt uisng mingw:
But i just want a solution to handle this by compiler by its own other than me adding pragma manually
#pragma is a hint for compiler. Compiler will not do it by default as in most cases it is not needed and reduces performance (so, it is a bad idea to enable it by default for all structs and I'm not aware of such a flag for the compiler). So, if you want to pack a struct then you have to use pragma, don't know why this is an issue for you.
-
I have structure with with 2 members one with data
struct xxx
{
std::uint32_t x;
std::uint8_t y;
};Now the issue is even if variable y is declared as 8 bit integer it is also occupying 32 bit. What I understood is , it is because of structure padding to maintain memory alignment.
Issue got resolved after adding #pragma pack(push,1)Is there any way to disable structure padding using some qt creator or compiler settings ?
@Parvathy2020 said in how to disable structure padding in qt uisng mingw:
Is there any way to disable structure padding using some qt creator or compiler settings ?
This has nothing to do with Qt.
You already found the correct solution using #pragma. -
@Parvathy2020 said in how to disable structure padding in qt uisng mingw:
Is there any way to disable structure padding using some qt creator or compiler settings ?
This has nothing to do with Qt.
You already found the correct solution using #pragma.@jsulm Hi...thanks for the quick reply. But i just want a solution to handle this by compiler by its own other than me adding pragma manually.
If not qt, can we configure mingw compiler to handle it . Any alternative solution other than adding it in code.
-
@jsulm Hi...thanks for the quick reply. But i just want a solution to handle this by compiler by its own other than me adding pragma manually.
If not qt, can we configure mingw compiler to handle it . Any alternative solution other than adding it in code.
@Parvathy2020 said in how to disable structure padding in qt uisng mingw:
But i just want a solution to handle this by compiler by its own other than me adding pragma manually
#pragma is a hint for compiler. Compiler will not do it by default as in most cases it is not needed and reduces performance (so, it is a bad idea to enable it by default for all structs and I'm not aware of such a flag for the compiler). So, if you want to pack a struct then you have to use pragma, don't know why this is an issue for you.
-
@Parvathy2020 said in how to disable structure padding in qt uisng mingw:
But i just want a solution to handle this by compiler by its own other than me adding pragma manually
#pragma is a hint for compiler. Compiler will not do it by default as in most cases it is not needed and reduces performance (so, it is a bad idea to enable it by default for all structs and I'm not aware of such a flag for the compiler). So, if you want to pack a struct then you have to use pragma, don't know why this is an issue for you.
@jsulm Thank you !
In my case this structure is auto - generated from a json file using some script. That is the challenge here- manually adding pragma to those auto generated structures.
-