我总是假设,正如他们在这里所说的,“重要的是要注意,最后一个成员是用所需的字节数填充的,这样结构的总大小应该是任何结构成员最大对齐的倍数”。
因此,对于这样的结构,在32个处理器上它的大小应该是16
typedef struct
{
double s; /* 8 bytes */
char c; /* 7 bytes padding at the end of make the total size 8*2 */
} structa_t;
所以我很惊讶它的尺寸是12而不是16!为什么会这样呢?有人能解释一下吗?
sizeof(double) = 8
sizeof
如何将下面的vc++打包命令翻译成Linux中的gcc命令?我知道如何在单个结构中做到这一点,但是如何在一系列结构中做到这一点?
#pragma pack(push, 1) // exact fit - no padding
//structures here
#pragma pack(pop) //back to whatever the previous packing mode was
假设我有一个C/C++结构
struct ABCTYPE
{
double A;
double B;
double C;
};
是否确保在任何类型的现代平台上都没有额外的填充?
更具体地说,如果我使用
std::vector<ABCTYPE> storage;
// add some elements
// now i need some C routines to the data
double* param=&(storage[0]);
c_routine(param); // Tested OK on Win/Linux, but will it wo