为什么青睐的数据结构调整? [英] Why favour data structure alignment?

查看:162
本文介绍了为什么青睐的数据结构调整?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该型结构的每个成员通常都有一个默认的对齐方式i.e.each结构件由pre确定的边界上对齐。
出于这个原因,在下面的维基示例进行填充:

The type of each member of the structure usually has a default alignment i.e.each structure member is aligned on a pre-determined boundary. For this reason the padding is performed in the following wiki example:

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 */
};

什么是(实用)的原因,调整应该是preserved?

What is the (practical) reason that alignment should be preserved?

推荐答案

非对齐读取和写入通常需要将CPU从存储器(而不是仅仅一个)取两个相邻字和应用一些附加的按位运算,以执行指定运行正常。

Unaligned reads and writes usually require the CPU to fetch the two adjacent words from memory (instead of just one) and to apply some additional bitwise arithmetic in order to perform the designated operation properly.

一些体系,像86将允许它以性能为代价。其他架构(最明显的是ARM),要么抛出一个异常(通常导致对用户进程 SIGBUS 信号),甚至圆圆的地址,到最近的界线可能会导致一些非常讨厌的错误。

Some architectures, like x86 will allow it at a performance cost. Other architectures (most notably ARM), will either raise an exception (usually resulting in a SIGBUS signal for a user process) or even "round" the address to the closest boundary which could result in some very nasty bugs.

这篇关于为什么青睐的数据结构调整?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆