Question: (Data Types). 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
(Data Types). 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 elements size. Suppose further that the compiler is not permitted to reorder fields. How much space will be consumed by the following array? Explain.
A : array [0..9] of record s : short ( 2 ) c : char ( 1 ) r : real ( 8 ) t : short ( 2 ) i : integer ( 4 )
PS: This is my answer, but is not correct completely. this is the comment I got from my professor: It is close. But check whether every primitive data element's starting address is an Even multiple of the element size.
My answer:

Let assume each block of memory is 8 bytes since the largest element is 8 bytes requirement: each element placed in the memory has to have an address which is a multiple of its size.
Step 1 : I placed element s (short) into the memory, which occupies 2 bytes out of the first block of size 8 bytes.
Step 2: I placed element c (char) into the memory, which occupies 1 bytes out of the first block of size 8 bytes. ( total of 3 bytes occupied till now)
step 3: I have to place r (real) into the block but since the block only have 5 bytes left and we are not allowed to do reorder, then I have to place the element r into the new block.
step 4: I placed element t (short) into the memory, which occupies 2 bytes out of the third block of size 8 bytes.
step 5: I have to place I (integer) into the memory, the third block, but since the address has to be multiple of elements size, I have to jump to 2 bytes and place the element i ( 4 bytes) into the last 4 bytes of the third block or in another word into the address 20 to 24. 24 will be multiple of 4.
Since our array has a range of 0-9 ( 10 elements) therefore the total space consumed will be 10 x 24 = 240 bytes.
8bytes empty empty
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
