What does the warning "alignment of a member was sensitive to packing" mean in C++? I'm using Visual Studio 2005.
How do I go about removing these warnings? I don't want to disable them btw.
Some data types must be aligned to a certain boundary. So for example:
struct V
{
char a;
double b;
char c;
double d;
};
sizeof(char) is 1 and sizeof(double) is 8 but the size of that struct may be more than the expected 18 if it needs the doubles to align to an 8-byte boundary. In that case, and because the members should appear in memory in the order they are declared in the struct, there may be 7 bytes of "padding" close to the member c, and possibly some with member a too.
The danger here comes when the packing is non-standard so the size of this struct could vary, and you send it in "binary" format over a wire or store it in a file where it will be read elsewhere (even if the endian-ness of the double is the same).
As an alternative to the suggestions to remove the warning through pragmas, you might decide to deal with it in the code by changing the order of your members. Put those that need the biggest alignment first, and the lower ones later. So first put pointers and doubles, then ints, then shorts and any char members last.
Taken from MSDN -
'symbol' : alignment of a member was sensitive to packing
A structure member is aligned on a memory offset whose value is not a multiple of the member's size. For example, the following code snippet will produce this warning:
// C4121.cpp
// compile with: /W4 /c
#pragma pack(2) // C4121
struct s
{
char a;
int b;
};
You could make one of the following changes to prevent this warning:
pack(2)
to pack(4)
.int
precedes the char
.When data is not aligned on boundaries that are multiples of the data's size performance can degrade and if you port your code to a RISC machine it will not compile.
You can specify the structure alignment with #pragma pack or /Zp. Note that the compiler does not generate this warning when /Zp1 is specified.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With