in Programming in C recategorized by
2,743 views
3 votes
3 votes

Suppose you are compiling on a machine with $1$-byte chars, $2$-byte shorts, $4$-byte ints, and $8$-byte doubles, and with alignment rules that require the address of every primitive data element to be an integer multiple of the element’s size. Suppose further that the compiler is not permitted to reorder fields; padding is used to ensure alignment. How much space will be consumed by the following array?

struct {
    short s;
    char c;
    short t;
    char d;
    double r;
    int i;
} A[10]; /*10 element array of structs */
  1. $150$ bytes
  2. $320$ bytes
  3. $240$ bytes
  4. $200$ bytes
in Programming in C recategorized by
2.7k views

3 Answers

13 votes
13 votes

The concept of alignment is used here.
The best trick is to make memory chunks of the highest primitive data type size which is here double.We consider our memory as chunks od 8Bytes.We will place the variable whether chat short int or double if the memory location is a multiple of its size.The left out space is wasted.this technique will improve the memory access time as CPU will each entire variable in each cycle
 

The first chunk of 8 Bytes

 short $s$ size is 2 bytes 2 divides 0 so its placed there and it occupies 0 and 1 byte
char  $c$ size is 1 Byte so it can start everywhere every number divides 1 so its placed at 2nd  byte
short  $t$  size is 2 bytes 2 divides 0 so its placed there and it cannot start at 3 so it  occupies 4  and  5th byte 
char  $d$ size is 1 Byte so it can start everywhere every number divides 1 so its placed at 6th   byte
 

The second chuck of 8 Bytes

Next comes the double which is of size 8 bytes it cannot start at 7th byte 
It (double $r$) starts from 8 and spans to 15
 

The third chuck of 8 Bytes



The int i will start from next location which is 16 so its valid and the bytes from 20 to 23 are wasted 

So each structure occupies $24$ Bytes space and there are $10$ such structures so total space is $240$ Bytes

Correct answer will be option c.
 

2 Comments

Best explanation
1
1
Great solution.
0
0
1 vote
1 vote

“The address of every primitive data element to be an integer multiple of the element’s size”, doesn’t make any sense as we can use any integer such as 1 as  a  multiple so we can store  it usual way.

So the total bytes needed for an element = 2+1+2+1+8+4 = 18 bytes

 

So  for array of 10 elements it should be 10* 18 = 180 bytes  (don’t match with any option)
 


Same question

https://stackoverflow.com/questions/4374276/alignment-rules (but here the alignment rule is,

The address is an even multiple of the element's size

)

And according to this the answer is 240 bytes option  (c)

2 Comments

Upon calculation i did got the answer of 180, but how did u calculated the even multiple

and how did we got 240 as the answer, can u please explain more in detail.
0
0
0
0
0 votes
0 votes
Hopefully this helps:

import pprint
l=[2,1,2,1,8,4]
count=0
i=0
d={}
while(count<10):
    for ele in l:
        while True:
            if(i%ele==0):
                d[i]=ele
                i=i+ele
                break
            i=i+1
    count+=1
pprint.pprint(d)  

 

______________

output:

{0: 2,
 2: 1,
 4: 2,
 6: 1,
 8: 8,
 16: 4,
 20: 2,
 22: 1,
 24: 2,
 26: 1,
 32: 8,
 40: 4,
 44: 2,
 46: 1,
 48: 2,
 50: 1,
 56: 8,
 64: 4,
 68: 2,
 70: 1,
 72: 2,
 74: 1,
 80: 8,
 88: 4,
 92: 2,
 94: 1,
 96: 2,
 98: 1,
 104: 8,
 112: 4,
 116: 2,
 118: 1,
 120: 2,
 122: 1,
 128: 8,
 136: 4,
 140: 2,
 142: 1,
 144: 2,
 146: 1,
 152: 8,
 160: 4,
 164: 2,
 166: 1,
 168: 2,
 170: 1,
 176: 8,
 184: 4,
 188: 2,
 190: 1,
 192: 2,
 194: 1,
 200: 8,
 208: 4,
 212: 2,
 214: 1,
 216: 2,
 218: 1,
 224: 8,
 232: 4}
Answer:

Related questions