对齐规则 [英] Alignment Rules

查看:108
本文介绍了对齐规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做作业时遇到了麻烦,我想知道是否有人能指出我正确的方向.

假设我们正在为一台机器编译 具有1个字节的字符,2个字节 短裤,4字节整数和8字节 实数,并具有对齐规则 要求每个原始地址 数据元素是的偶数倍 元素的大小.进一步假设 不允许编译器 重新排序字段.多少空间 被以下数组消耗了吗?

A : array [0..9] of record
    s : short;
    c : char;
    t : short;
    d : char;
    r : real;
    i : integer;
end;

现在,我基本上了解了这个问题,但是真正让我陷入困境的是需要每个原始数据的地址的对齐规则" 元素要等于元素大小的偶数".关于对齐规则,我的书不是很清楚,说实话,对于偶数是什至我也不是很肯定.任何帮助将不胜感激.

此外,我相信答案是240字节,我只需要一些帮助即可到达那里.

解决方案

让我们分解一下:

对齐规则",要求每个基本数据元素的地址为元素大小的偶数".我们谈论对齐规则不是很有趣.我们已经知道了.

每个原始数据元素"的要求地址"为元素大小的偶数".现在我们到了某个地方.我们有一个要求和一个范围:

Requirement: The address is an even multiple of the element's size.
Scope: Every primitive data element.

因此,每次定位元素时,都必须强加要求.

让我们尝试将元素放置在内存中.我们要定位的第一件事是标记为sshort.由于short占用2个字节的内存,并且我们必须使其地址为该大小的倍数,因此该地址必须为2的倍数.我们将该地址称为N.

因此,s占用了从NN + 2的空间. (注意:对于所有这些范围,都包括第一个端点,但不包括最后一个端点.这是计算机科学中描述整数范围的常规方法;在大多数情况下,到目前为止最有用,最不容易出错的方法.相信我.)

我们继续互相探讨.

c占用一个字节,从N + 2N + 3.

我们在N + 3,但是由于N + 3是奇数(因为N是偶数)所以我们不能在这里开始t.因此,我们必须跳过一个字节.因此t的范围是N + 4N + 6.

继续这种逻辑,我们得到dN + 6N + 7rN + 8N + 16; iN + 16N + 20. (注意,这仅在我们将N限制为8的倍数时有效,否则r将是未对齐的.这没关系;当我们为数组分配内存时,我们可以对齐起始位置但是我们想要-我们只需要保持一致就可以保证数据顺序.)

因此,此结构至少需要 20个字节. (这是半开范围的优势之一:端点之间的差异等于大小.如果我们将两个端点包括在范围内或从该范围中排除,则必须进行+1或-1校正.)

现在让我们说我们尝试将数组布置为20个字节的十个连续块.这会工作吗?不;说元素0在地址256(8的倍数)处.现在元素1中的r将是未对齐的,因为它将以256 + 20 + 8开头,不能被8整除.这是不允许的.

那我们现在该怎么办?我们不能只在元素1的r之前插入额外的4个字节,因为数组的每个元素都必须具有相同的布局(更不用说大小)了.但是有一个简单的解决方案:我们在每个元素的 end 处插入4个字节的附加填充.现在,只要数组以8的倍数开始,每个元素也将以8的倍数开始(依次保持r对齐),因为大小现在是8的倍数.

我们得出的结论是,结构需要24个字节,因此数组需要24 * 10 = 240个字节.

I'm having a bit of trouble with a homework problem, and I was wondering if anyone could point me in the right direction.

Suppose we are compiling for a machine with 1-byte characters, 2-byte shorts, 4-byte integers, and 8-byte reals, and with alignment rules that require the address of every primitive data element to be an even multiple of the element’s size. Suppose further that the compiler is not permitted to reorder fields. How much space will be consumed by the following array?

A : array [0..9] of record
    s : short;
    c : char;
    t : short;
    d : char;
    r : real;
    i : integer;
end;

Now I understand the problem, for the most part, but the thing that is really throwing me for a loop is the "alignment rules that require the address of every primitive data element to be an even multiple of the element’s size". My book isn't very description when it comes to alignment rules and to be completely honest, I'm not even positive on what an even multiple is. Any help would be appreciated.

Also, I believe the answer is 240-bytes, I just need some help getting there.

解决方案

Let's break that down:

"alignment rules" that "require the address of every primitive data element to be an even multiple of the element’s size". It's not very interesting that we're talking about alignment rules; we knew that already.

"require the address" of "every primitive data element" to be "an even multiple of the element’s size". Now we're getting somewhere. We have a requirement and a scope:

Requirement: The address is an even multiple of the element's size.
Scope: Every primitive data element.

So, every time we position an element, we must impose the requirement.

Let us try to position an element in memory. The first thing we will position is the short labelled s. Since a short takes up 2 bytes of memory, and we must make its address a multiple of that size, the address must be a multiple of 2. Let's call that address N.

So, s takes up the space from N up to N + 2. (NOTE: For all of these ranges, the first endpoint is included, but the last endpoint is not. This is the normal way to describe ranges of integers in computer science; in most cases it is by far the most useful and least error-prone way to do it. Trust me.)

We continue with each other field.

c takes up one byte, from N + 2 to N + 3.

We are at N + 3, but we cannot start t there, because N + 3 is odd (since N is even). So we must skip a byte. Thus t ranges from N + 4 to N + 6.

Continuing with this sort of logic, we end up with d from N + 6 to N + 7; r from N + 8 to N + 16; i from N + 16 to N + 20. (NOTE that this only works if we restrict N to be a multiple of 8, or r will be unaligned. This is ok; when we allocate the memory for the array, we can align the start of it however we want - we just have to be consistent about the sequence of data after that point.)

So we need at least 20 bytes for this structure. (That's one of the advantages of the half-open ranges: the difference between the endpoints equals the size. If we included or excluded both endpoints from the range, we'd have to make a +1 or -1 correction.)

Now let's say we try to lay out the array as ten consecutive chunks of 20 bytes. Will this work? No; say that element 0 is at address 256 (a multiple of 8). Now r in element 1 will be unaligned, because it will start at 256 + 20 + 8, which is not divisible by 8. That's not allowed.

So what do we do now? We can't just insert an extra 4 bytes before r in element 1, because every element of the array must have the same layout (not to mention size). But there is a simple solution: we insert 4 bytes of additional padding at the end of each element. Now, as long as the array starts at some multiple of 8, every element will also start at a multiple of 8 (which, in turn, keeps r aligned), because the size is now a multiple of 8.

We conclude that we need 24 bytes for the structure, and thus 24 * 10 = 240 bytes for the array.

这篇关于对齐规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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