为了获得最大的性能(良好的内存对齐),我总是在C中填充我的结构体。
// on a x86_64
struct A {
int64_t z;
int32_t x;
int16_t y;
// we don't care the uint8_t position, as they are 1 byte wide.
uint8_t s[8];
uint8_t t[4];
}
但是如果我决定走c++的路线,为了其他目的创建一个对象,我需要一个类:
class B {
B(){}
~B(){}
public:
int64_t a;
int8_t b;
private:
int32_t c;
//methods...
}
那么,c就不再对齐了。
有没有方法可以避免这种情况(多个标签):
class B {
B(){}
~B(){}
public:
int64_t a;
private:
int32_t c;
public:
int8_t b;
}
在一些 CPU 上,对齐很重要。谢谢。