edited by
499 views
3 votes
3 votes

Consider struct alignment rules as described below –

  1. Each primitive data (int, char, float, etc.) type requiring $\text{K}$ bytes must be stored at the address must be multiple of $\text{K}.$
  2. Address of structure and structure length must be multiples of $\text{K}_{\max}.$ Where $\text{K}_{\max}$ is the largest size of any primitive element of the struct.

The sizes are as follows -

sizeof (long int)=8 bytes
sizeof (int)=4 byte
sizeof (short int)=2 bytes

struct s{
    short int a [5];
    long int b;
}

What will be $\text{sizeof(struct s)}?$

  1. $24$
  2. $18$
  3. $32$
  4. $20$
edited by

3 Answers

1 votes
1 votes

short int a[5] takes : 10B

& long int b : 8B : it should be place at start of location which is multiple of 8 : 16

so,

1 to 10B : short int a[5]

11 to 15 : padding

16 to 24 : “long int b”

edited by
0 votes
0 votes
short int a [5];

 Takes 10 B

Now to satisfy Condition 1 , +6B of memory alignment needs to be done so that Long can start from 16 as it a multiple of 8

now 16+8=24

Answer:

Related questions

4 votes
4 votes
3 answers
3
GO Classes asked Aug 6, 2022
731 views
Consider the following declaration of variables $a$ and $b.$int a = 1, b = 0;Which of the following is/are will evaluate to TRUE?b++ && b == ab++ && a == 0a || b == aa |...