gpt4 book ai didi

c - C 编译器使用的数据布局(对齐概念)

转载 作者:行者123 更新时间:2023-12-04 07:52:29 25 4
gpt4 key购买 nike

下面是红龙书的摘录。

Example 7.3. Figure 7.9 is a simplification of the data layout used by C compilers for two machines that we call Machine 1 and Machine 2.

Machine 1 : The memory of Machine 1 is organized into bytes consisting of 8 bits each. Even though every byte has an address, the instruction set favors short integers being positioned at bytes whose addresses are even, and integers being positioned at addresses that are divisible by 4. The compiler places short integers at even addresses, even if it has to skip a byte as padding in the process. Thus, four bytes, consisting of 32 bits, may be allocated for a character followed by a short integer.

Machine 2: each word consists of 64 bits, and 24 bits are allowed for the address of a word. There are 64 possibilities for the individual bits inside a word, so 6 additional bits are needed to distinguish between them. By design, a pointer to a character on Machine 2 takes 30 bits — 24 to find the word and 6 for the position of the character inside the word. The strong word orientation of the instruction set of Machine 2 has led the compiler to allocate a complete word at a time, even when fewer bits would suffice to represent all possible values of that type; e.g., only 8 bits are needed to represent a character. Hence, under alignment, Fig. 7.9 shows 64 bits for each type. Within each word, the bits for each basic type are in specified positions. Two words consisting of 128 bits would be allocated for a character followed by a short integer, with the character using only 8 of the bits in the first word and the short integer using only 24 of the bits in the second word. □

the figure


我发现了关于对齐的概念 here , herehere .我可以从它们中理解如下:在字可寻址 CPU(大小超过一个字节)中,在数据对象中引入了某些填充,以便 CPU 可以有效地从内存中检索数据,而最少没有。内存周期。
现在 Machine 1这里实际上是一个字节地址。以及 Machine 1中的条件规范可能比字大小为 4 的简单字可寻址机器更困难。字节。在这样的 64位机,我们需要确保我们的数据项只是字对齐,没有更多的困难。但是如何在 Machine 1 等系统中找到对齐方式(如上表所示)其中字对齐的简单概念不起作用,因为它是字节可寻址的并且具有更困难的规范。
此外,我发现在 double 行中很奇怪。类型的大小大于对齐字段中给出的大小。不应该 alignment(in bits) ≥ size (in bits) ?因为对齐是指实际为数据对象分配的内存(?)。
“每个字由 64 位组成,并且 24 位被允许用于一个字的地址。一个字内的各个位有 64 种可能性,因此需要 6 额外位来区分它们. 按照设计,指向 Machine 2 上的字符的指针需要 30 位 — 24 来查找单词,6 表示字符在单词中的位置。” - 此外,关于指针概念的这个陈述应该如何可视化,基于对齐(2^6 = 64,这很好,但这 6 位与对齐概念如何相关)

最佳答案

首先,机器 1 一点都不特别——它和 x86-32 或 32 位 ARM 完全一样。

Moreover I find it quite weird that in the row for double the size of the type is more than what is given in the alignment field. Shouldn't alignment(in bits) ≥ size (in bits) ? Because alignment refers to the memory actually allocated for the data object (?).


不,这不是真的。对齐意味着对象中最低可寻址字节的地址必须能被给定的字节数整除。
此外,对于 C,在数组 sizeof (ElementType) 中也是如此。将需要是 大于或等于 到每个成员的对齐 sizeof (ElementType)可以被对齐整除,因此脚注 a.因此在后一台计算机上:
 struct { char a, b; }
可能有大小 16 因为字符在不同的可寻址词中,而
struct { char a[2];  }
可以压缩成8个字节。

how should this statement about the concept of the pointers, based on alignment is to be visualized (2^6 = 64, it is fine but how is this 6 bits correlating with the alignment concept)


至于字符指针,6位是假的。需要 3 位来选择 之一8 中的字节数8 字节 话,所以这是书中的错误。一个普通的字节只会选择一个 24 位的字,一个字符(一个字节)指针会选择一个 24 位的字,以及字内 3 位的 8 位字节之一。

关于c - C 编译器使用的数据布局(对齐概念),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66884143/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com