I just learned about padding, and I was trying to do some tests about it, I tried to pack this struct :
struct B {
int a,b,c;
string s;
char x;
string t;
char y;
string u;
}__attribute__((packed)) ;
But I get this Warning :
warning: ignoring packed attribute because of unpacked non-POD field 'std::string B::u'
string u;
Does this mean that structs containing strings
cannot be packed ? Is there any other way to do it ? if so does it affect the performance ?
A good rule of thumb is to sort your members from biggest to smallest. That way your data is aligned and (usually) has no gaps. E.g. on VS2013 for an x64 target the following layout requires 112 instead of 128 bytes:
struct B {
string s,t,u;
int a,b,c;
char x,y;
};
For an x86 target however, this only saves you 4 bytes. Whether or not and how this impacts your performance depends on so many other factors, that it can only be determined by measurement.
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