什么是数据对齐?在C中类型转换指针时,为什么以及何时应该担心? [英] What is data alignment? Why and when should I be worried when typecasting pointers in C?

查看:89
本文介绍了什么是数据对齐?在C中类型转换指针时,为什么以及何时应该担心?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到合适的文档来说明对齐系统的工作原理,以及为什么某些类型比其他类型更严格地对齐.

I couldn't find a decent document that explains how the alignment system works and why some types are more strictly aligned than the others.

推荐答案

我将尝试简单地解释一下.

I'll try to explain in short.

计算机中的体系结构由处理器和内存组成. 内存是按单元组织的,所以:

The architecture in you computer is composed of processor and memory. Memory is organized in cells, so:

 0x00 |   data  |  
 0x01 |   ...   |
 0x02 |   ...   |

每个存储单元都有指定的大小,可以存储的位数.这取决于体系结构.

Each memory cell has a specified size, amount of bits it can store. This is architecture dependent.

在C/C ++程序中定义变量时,程序会占用一个或更多个不同的单元格.

When you define a variable in your C/C++ program, one or more different cells are occupied by your program.

例如

int variable = 12;

假设每个单元格包含32位,并且int类型大小为32位,然后在内存中的某个位置:

Suppose each cell contains 32 bits and the int type size is 32 bits, then in somewhere in your memory:

variable: | 0 0 0 c |  // c is hexadecimal of 12.

当CPU必须对该变量进行操作时,需要将其带入其寄存器中. CPU可以从内存中获取少量的" 1个时钟",该大小通常称为 WORD .此维度也取决于体系结构.

When your CPU has to operate on that variable it needs to bring it inside its register. A CPU can take in "1 clock" a small amount of bit from the memory, that size is usually called WORD. This dimension is architecture dependent as well.

现在假设您有一个变量,由于某些偏移量,该变量存储在两个单元格中.

Now suppose you have a variable which is stored, because of some offset, in two cells.

例如,我有两个不同的数据要存储(我将使用"字符串表示法使之更清晰"):

For example I have two different pieces data to store (I'm going to use a "string representation to make more clear"):

data1: "ab"
data2: "cdef"

因此,内存将以这种方式组成(2个不同的单元格):

So the memory will be composed in that way (2 different cells):

|a b c d|     |e f 0 0|

也就是说,data1仅占据单元格的一半,因此data2占据第二个单元格的剩余部分和一部分.

That is, data1 occupies just half of the cell, so data2 occupies the remaining part and a part of a second cell.

现在假设您的CPU要读取data2. CPU需要 2个时钟来访问数据,因为它在一个时钟内读取了第一个单元,而在另一个时钟内读取了第二个单元的其余部分.

Now suppose you CPU wants to read data2. The CPU needs 2 clocks in order to access the data, because within one clock it reads the first cell and within the other clock it reads the remaining part in the second cell.

如果按照此内存示例 align data2,我们可以 在第二个单元格中引入一种 padding 并移动data2.

If we align data2 in accordance with this memory-example, we can introduce a sort of padding and shift data2 all in the second cell.

|a b 0 0|     |c d e f|
     ---
   padding

通过这种方式,CPU将仅丢失" 1个时钟"以访问data2.

In that way the CPU will lose only "1 clock" in order to access to data2.

align系统只是引入了 padding ,以便将数据与系统内存对齐,请记住,要遵循体系结构.当数据在内存中对齐时,您不会浪费CPU周期来访问数据.

An align system just introduces that padding in order to align the data with the memory of the system, remember in accordance with the architecture. When the data is aligned in the memory you don't waste CPU cycles in order to access the data.

这样做是出于性能方面的考虑(99%的时间).

This is done for performance reasons (99% of times).

这篇关于什么是数据对齐?在C中类型转换指针时,为什么以及何时应该担心?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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