What would be the memory requirement for variable t, ignoring alignment consideration?

Consider the following C declaration

struct ( 
    short x[5];  
    union { 
        float y; 
        long z; 
    } u;
 )t;

Assume that the objects of the type short, float and long occupy 2 bytes, 4 bytes and 8 bytes, respectively. The memory requirement for variable t, ignoring alignment consideration, is

  1. 22 bytes
  2. 14 bytes
  3. 18 bytes
  4. 10 bytes
1Comment
shivani @shivani1234
17 Apr 2017 11:24 am

Basically , two things we need to know to solve this question:

  1. Size of structure is the sum of all its members size
  2. Size of union is maximum among all its member size.

So,coming to problem we analyse that t is structure having x[5], union u as its member , and u is union having y and z as its member.

Size of u is max(sizeof(y), sizeof(z)) which is max(4,8) =8 Bytes

Size of t is sum of all its members which is (sizeof(x), sizeof(u))

here, as x is an array of type short having 5 elements , so its size is (5*2)= 10 Bytes

thus t=(10+8) =18 Bytes.

 

Pages