retagged by
3,329 views
2 votes
2 votes

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[10]

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$
retagged by

3 Answers

4 votes
4 votes

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

1 votes
1 votes

$\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}$


http://www.firmcodes.com/structure-padding-and-packing-in-c-example/

edited by
1 votes
1 votes

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

Answer:

Related questions

6 votes
6 votes
5 answers
2
Satbir asked Jan 13, 2020
3,713 views
What is the output of the code given below?# include<stdio.h int main() { char name[]="satellites"; int len; int size; len= strlen(name); size = sizeof(name); printf("%d"...
2 votes
2 votes
3 answers
3
Satbir asked Jan 13, 2020
1,786 views
Consider the following recursive C function that takes two argumentsunsigned int rer(unsigned int n, unsigned int r){ if(n>0)return(n%r + rer(n/r,r)); else retturn 0; }Wh...
6 votes
6 votes
1 answer
4