Yak*_*kov 5 c c++ memory struct
结构的每个成员的类型通常具有默认对齐,即每个结构成员在预定边界上对齐.因此,填充是在以下wiki示例中执行的:
struct MixedData
{
char Data1;
short Data2;
int Data3;
char Data4;
};
struct MixedData /* After compilation in 32-bit x86 machine */
{
char Data1; /* 1 byte */
/* 1 byte for the following 'short' to be aligned on a 2 byte boundary
assuming that the address where structure begins is an even number */
char Padding1[1];
short Data2; /* 2 bytes */
int Data3; /* 4 bytes - largest structure member */
char Data4; /* 1 byte */
char Padding2[3]; /* 3 bytes to make total size of the structure 12 bytes */
};
Run Code Online (Sandbox Code Playgroud)
应该保留对齐的(实际)原因是什么?
在许多体系结构中,与主存储器对齐的读取和写入比未对齐的对应物快得多.
未对齐的读取和写入通常需要CPU从存储器(而不是仅仅一个)中获取两个相邻的字,并应用一些额外的逐位算术以正确执行指定的操作.
某些体系结构(如x86)将以性能成本实现.其他架构(最值得注意的是ARM)将引发异常(通常导致SIGBUS
用户进程的信号)或甚至将地址"舍入"到最近的边界,这可能导致一些非常讨厌的错误.
归档时间: |
|
查看次数: |
627 次 |
最近记录: |