什么是字节对齐,为什么要对齐?

壹点灵异 壹点灵异     2022-09-25     584

关键词:

Computer Systems: A Programmer‘s Perspective:

3.9.3 Data Alignment

Many computer systems place restrictions on the allowable addresses for the primitive data types, requiring that the address for some type of object must be a multiple of some value K (typically 2, 4, or 8). Such alignment restrictions simplify the design of the hardware forming the interface between the processor and the memory system. For example, suppose a processor always fetches 8 bytes from memory with an address that must be a multiple of 8. If we can guarantee that any double will be aligned to have its address be a multiple of 8, then the value can be read or written with a single memory operation. Otherwise, we may need to perform two memory accesses, since the object might be split across two 8-byte memory blocks.

The IA32 hardware will work correctly regardless of the alignment of data. However, Intel recommends that data be aligned to improve memory system performance. Linux follows an alignment policy where 2-byte data types (e.g., short) must have an address that is a multiple of 2, while any larger data types (e.g., int, int *, float, and double) must have an address that is a multiple of 4. Note that this requirement means that the least significant bit of the address of an object of type short must equal zero. Similarly, any object of type int, or any pointer, must be at an address having the low-order 2 bits equal to zero.

Aside: A case of mandatory alignment

For most IA32 instructions, keeping data aligned improves efficiency, but it does not affect program behavior. On the other hand, some of the SSE instructions for implementing multimedia operations will not work correctly with unaligned data. These instructions operate on 16-byte blocks of data, and the instructions that transfer data between the SSE unit and memory require the memory addresses to be multiples of 16. Any attempt to access memory with an address that does not satisfy this alignment will lead to an exception, with the default behavior for the program to terminate.

This is the motivation behind the IA32 convention of making sure that every stack frame is a multiple of 16 bytes long (see the aside of page 226). The compiler can allocate storage within a stack frame in such a way that a block can be stored with a 16-byte alignment.

Aside: Alignment with Microsoft Windows

Microsoft Windows imposes a stronger alignment requirement—any primitive object of K bytes, for K = 2, 4, or 8, must have an address that is a multiple of K. In particular, it requires that the address of a double or a long long be a multiple of 8. This requirement enhances the memory performance at the expense of some wasted space. The Linux convention, where 8-byte values are aligned on 4-byte boundaries was probably good for the i386, back when memory was scarce and memory interfaces were only 4 bytes wide. With modern processors, Microsoft’s alignment is a better design decision. Data type long double, for which gcc generates IA32 code allocating 12 bytes (even though the actual data type requires only 10 bytes) has a 4-byte alignment requirement with both Windows and Linux.
 
 
作者:赵劼
链接:https://www.zhihu.com/question/23791224/answer/25671861
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

什么是 Core Animation 的字节对齐(缓存行对齐)?为什么重要?

】什么是CoreAnimation的字节对齐(缓存行对齐)?为什么重要?【英文标题】:whatisbytealignment(cachelinealignment)forCoreAnimation?Whyitmatters?【发布时间】:2014-05-2118:14:25【问题描述】:我以非惰性方式在滚动视图上加载图像,因此看不... 查看详情

非对齐访问和alignmentfault

...对齐的。如果是char类型的,那就没有没有对齐要求了。为什么在部分硬件上出现?部 查看详情

内存对齐详解(c++代码)(代码片段)

...关:C++内存管理,分了多少段,在堆上和在栈上的区别,为什么要区分堆和栈,new和malloc的区别,内存对齐,为什么会出现内存对齐问题,程序员为什么要关注内存对齐问题内存对齐是指数据在内存中的存储方式,要求数据存储... 查看详情

八字节对齐(代码片段)

...跨平台或者网络通信的时候,都会要求进行字节对齐,那为什么需要对齐,如果不对齐会有什么问题呢。  (1)存储方式:    现代计算机处理器对存储的读取都是按照特定大小字节去读写(称其为一个存储单元),比... 查看详情

字节对齐

...要求是一个对齐模数(alignmentmodulus)的整数倍。问题来了,为什么要有这种策略?计算中内存中的数据就是一个一个的字节(byte),直接按照一个字节一个字节存储就得了,为什么还要那么麻烦。把问题想简单了。各个硬件平台... 查看详情

字节对齐

字节对齐为什么字节对齐?一个字或双字操作数跨越了4字节边界,或者一个四字操作数跨越了8字节边界,被认为是未对齐的,从而需要两次总线周期来访问内存。一个字起始地址是奇数但却没有跨越字边界被认为是对齐的,能... 查看详情

字节对齐

什么是字节对齐cpu在访问存储器的时候,通常从存储器中取出固定长度的字节数。计算机系统为了配合提高cpu的访问效率,也规定数据类型通常会是2或4的倍数,以简化cpu做取数据动作的复杂程度。cpu取数据是以地址为单位的,... 查看详情

字节对齐

...一个整型变量的地址为0x00000004,那它就是自然对齐的。为什么要字节对齐需要字节对齐的根本原因在于CPU访问数据的效率问题。例如,假设一个处理器在读取数据的时候,比如他要读取一个long类型的数据,因为long长度为8字节... 查看详情

“现在打包强制记录的字节对齐”是啥意思?

...ignmentofRecords"mean?“现在打包强制记录的字节对齐”是什么意思?【发布时间】:2012-01-1715:40:04【问题描述】:DelphiXE2的新增功能包含following。PackedNow强制记录的字节对齐如果您有使用打包记录类型的旧代码并且您希望要与外... 查看详情

理解单词对齐

...】:我理解访问内存以使其对齐意味着什么,但我不明白为什么这是必要的。例如,为什么我可以从地址0x…1访问单个字节,但我不能从同一地址访问半字(两个字节)。我再次了解,如果您有一个地址A和一个大小为s的对象,... 查看详情

硬盘4k对齐

...据。那么如果有“4K对齐”一说必然就有“4K对不齐”。为什么会有“4K”对不齐呢?这是因为在NTFS6.x以前的规范中,数据的写入点正好会介于在两个4K扇区的之间,也就是说即使是写入最小量的数据,也会使用到两个4K扇区,显然... 查看详情

4096个字节是4k对齐了吗?

4096个字节只是4k大小,并不一定能保证其内存是4k对齐的。在系统内存足够的情况下,使用malloc等函数分配内存一般也能得到内存对齐的起始地址,但是如果有要求4k(或512字节)对齐时,最好使用专用的API来分配内存。在VS中使... 查看详情

c语言字节对齐(代码片段)

...地址为0x00000004,那它就是自然对齐的。    二、为什么要字节对齐  查看详情

结构体内存对齐

参考技术A1.什么是内存对齐?2.为什么要做内存对齐?3.结构体内存对齐规则4.源码内存对齐算法计算机内存都是以字节为单位划分的,从理论上讲似乎对任何类型的变量的访问可以从任何地址开始,但是实际的计算机系统对基本类... 查看详情

深信服凉经(代码片段)

...一个线程对链表加,一个对链表减。4.什么是字节对齐?为什么要字节对齐?为什么字节对齐会比较快?5.new和malloc的区别,,它们没申请到内存会出现什么情况?6.oj评测系统如何防止别人提交搞破坏的代码?7.为什么有时上传大... 查看详情

字节对齐

...C++和C语言中让我们头疼的字节对齐问题:一、首先来看什么是字节对其?     现代计算机中内存空间都是按照byte划分的,从理论上讲似乎对任何类型的变量的访问可以从任何地址开始,但实际情况是在访问特... 查看详情

字节对齐1

一、快速理解1、什么是字节对齐?  在C语言中,结构是一种复合数据类型,其构成元素既可以是基本数据类型(如int、long、float等)的变量,也可以是一些复合数据类型(如数组、结构、联合等)的数据单元。在结构中... 查看详情

chatgpt教我内存对齐,对齐了但没完全对齐?(代码片段)

...如下:我现在来描述与总结上述对话都干了啥以及我为什么要问这个。我本来是在学习rapidjson源码里面的内存池实现,然后RAPIDJSON_ALIGN没有看懂,所以来问chatgpt。源码在:github.com/Tencent/rapidjson/blob/master/include/rapid... 查看详情