1,957 views

Following declaration of an array of struct, assumes size of byte, short, int and long are $1,2,3$ and $4$ respectively. Alignment rule stipulates that $n$ – byte field must be located at an address divisible by $n$, the fields in the struct are not rearranged, padding is used to ensure alignment. All elements of array should be of same size.

Struct complx
Short s
Byte b
Long l
Int i
End Complx
Complx C

Assuming $C$ is located at an address divisble by $8$, what is the total size of $C$, in bytes?

1. $150$
2. $160$
3. $200$
4. $240$

### 1 comment

None are correct. Question cancelled by ISRO.

I believe the options are incorrect

Alignment rule stipulates that $n$−byte field must be located at an address divisible by $n$.

So,

• $Short$ must be in an address divisible by $2$
• $Byte$ must be in an address divisible by $1$
• $Long$ must be in an address divisible by $4$
• $Int$ must be in an address divisible by $3$

Following the same (assuming Base Address to be $0$),

 $Short$ $Byte$ - $Long$ - $Int$ $0$ $2$ $3$ $4$ $8$ $9$

So the data type is $12$ bytes after padding. Since it's an array of $10$ elements, total size = $120$ bytes

by

That's why ISRO dropped this question
great explanation.(good job man).

$\underline{\mathbf{Answer:}\Rightarrow}$

$\underline{\mathbf{Explanation:}\Rightarrow}$

$\underline{\mathrm{Padding:}\Rightarrow}$

For aligning the data in the memory, some empty bytes are inserted between the memfory addresses which are assigned for other structure members during memdory allocation.

This is called the structure padding.

Size of the complex data type will be $2+1+4+3 = 10\;\text{Bytes}$

But because of padding, size will become $\mathbf{16}-\text{Bytes}\;\;\text{Since,$6$Bytes are added for padding}]$

$\therefore$ Total size $=16\times 10 = 160 \;\text{Bytes}$

by

Size of Byte = 1

Size of Short= 1

Size of Int= 1

Size of Long= 1

Total Size of Complex Structure = 10

Alignment Rule for N byte :- N Byte mod N = 0

This C code is located at an address which is divisible by N = 8

So, how many more(x) Bytes should we add to get (10 + x) mod 8 = 0

Here Size of Byte is 1 so keep add Bytes until we get (10 + x) mod 8 = 0

(10+1) mod 8 = 3

(10+2) mod 8 = 4

(10+3) mod 8 = 5

(10+4) mod 8 = 6

(10+5) mod 8 = 7

(10+6) mod 8 = 0

So, we need 16 Bytes ( 6 Bytes Padding )

Total 16*10 = 160 Bytes