in Programming recategorized by
1,861 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 recategorized by
1.9k views

2 Answers

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