Below is an excerpt from the red dragon book.
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
andMachine 2
.
Machine 1
: The memory ofMachine 1
is organized into bytes consisting of 8 bits each. Even though every byte has an address, the instruction set favorsshort
integers being positioned at bytes whose addresses are even, and integers being positioned at addresses that are divisible by4
. 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 of32
bits, may be allocated for a character followed by a short integer.
Machine 2
: each word consists of64
bits, and24
bits are allowed for the address of a word. There are64
possibilities for the individual bits inside a word, so6
additional bits are needed to distinguish between them. By design, a pointer to a character onMachine 2
takes30
bits —24
to find the word and6
for the position of the character inside the word. The strong word orientation of the instruction set ofMachine 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., only8
bits are needed to represent a character. Hence, under alignment, Fig. 7.9 shows64
bits for each type. Within each word, the bits for each basic type are in specified positions. Two words consisting of128
bits would be allocated for a character followed by a short integer, with the character using only8
of the bits in the first word and the short integer using only24
of the bits in the second word. □
I found about the concept of alignment here ,here and here. What I could understand from them is as follows: In word addressable CPUs (where size is more than a byte), there certain paddings are introduced in the data objects, such that CPU can efficiently retrieve data from the memory with minimum no. of memory cycles.
Now the Machine 1
here is actually a byte address one. And the conditions in the Machine 1
specification are probably more difficult than a simple word addressable machine having word size of say 4
bytes. In such a 64
bit machine, we need to make sure that our data items are just word aligned ,no more difficulty. But how to find the alignment in systems like Machine 1
(as given in the table above) where the simple concept of word alignment does not work, because it is byte addressable and has much more difficult specifications.
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 (?).
"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." - Moreover 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)