retagged by
7,368 views
5 5 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$

5 Answers

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

8 8 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 flag:
✌ Edit necessary (yash mishra “solution not completely correct”)
4 4 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

0 0 votes

1. Place short s (2 bytes)

  • Struct starts at address 0 (divisible by 8 already given).

  • Needs multiple of 2 → address 0 is fine.

0 1 [s][s]

We’re at offset 2 now.


2. Place byte b (1 byte)

  • Next free address = 2 → works (1-byte alignment).

2 [b]

We’re at offset 3 now.


3. Place long l (4 bytes)

  • Needs address divisible by 4.

  • Next free = 3 → NOT divisible by 4.

  • Add 1 byte padding to reach 4.

3 <- pad 4 5 6 7 [l][l][l][l]

We’re at offset 8 now.


4. Place int i (3 bytes)

  • Needs address divisible by 3.

  • Next free = 8 → NOT divisible by 3.

  • Add 1 byte padding to reach 9.

8 <- pad 9 10 11 [i][i][i]

We’re at offset 12 now.


5. End padding for struct size

  • We want every struct in an array to start at an address divisible by 8 (biggest alignment requirement given).

  • Next multiple of 8 after 12 is 16.

  • So add 4 bytes padding at end.


0-1 : s 2 : b 3 : pad 4-7 : l 8 : pad 9-11 : i 12-15 : pad (end)

Size of one struct = 16 bytes.

we have 10 structs:

10 × 16 = 160 bytes total.

So the correct answer is B. 160 bytes.

Answer:
Position:
Show:

Related questions

9 9 votes
10 answers 10 answers
19.8k
19.8k views
Satbir asked Jan 13, 2020
19,780 views
Consider a $2$-dimensional array $x$ with $10$ rows and $4$ columns, with each element storing a value equivalent to the product of row number and column number. The arra...
9 9 votes
5 5 answers
7.5k
7.5k views
Satbir asked Jan 13, 2020
7,469 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"...
7 7 votes
4 4 answers
3.8k
3.8k views
Satbir asked Jan 13, 2020
3,759 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...
10 10 votes
1 1 answer
7.1k
7.1k views
Satbir asked Jan 13, 2020
7,113 views
In the following procedureInteger procedure P(X,Y); Integer X,Y; value x; begin K=5; L=8; P=x+y; end$X$ is called by value and $Y$ is called by name. If the procedure wer...