in Programming in C edited by
20,710 views
51 votes
51 votes

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
in Programming in C edited by
20.7k views

4 Comments

What is meant by “ignoring alignment consideration” in the question.

Output in c compiler gives 24 as the correct answer. Is the above statement reason for answer to be 18.
0
0
edited by

For the given question 

short 2 Bytes

float 4 bytes,

long 8 bytes.

 

With considering alignment(calculated. Not run on any compiler).

On 32 but machine

2*5+6(padding)+8 = 24 Bytes. But whole structure also need padding. Largest member is short x[5] that is 10 Bytes. So, answer should be in multiple of 10.

Means 30 Bytes.

I have read from https://gateoverflow.in/245263/structure-alignment?show=245419#c245419

He is giving answer 30 Bytes for 32 bits.

@Shiva Sagar Rao

Also has same question as yours : memory size remains same for both 32 bit and 64 bit architecture ?

means same answer(30 Bytes) for 64 bit machine.

 

Please, verify answer is same 30 Bytes for both 32 bits and 64 bits machine ? @Shaik Masthan

It is confusing. 

 

P.S. : On running on my machine. with changing long to long long to get that same configuration 2,4,8 as in question. I got 24 Bytes.(not 30).

Also check this https://ideone.com/AnHYF7 gives 24 Bytes. What about structure’s itself padding ?

0
0
What will be the memory requirement for variable u?
0
0

4 Answers

62 votes
62 votes
Best answer

Correct Option: C

Here, structure creates the memory for '$\text{array and union}$', but union only creates the memory for only '$\text{long z}$' which is the largest size data type inside it.

Hence,

$short \times [5] = 5*2 = 10$ bytes  [ shorts take $2$ bytes]

$\text{long z}  = 8$ bytes

So, ($10+8) = 18$ bytes

edited by

4 Comments

Sir, why *UNION* only conside the max?
1
1
I too agree with you.
0
0
Agreed..!
0
0
29 votes
29 votes

When memory would be assigned for the array 

short x[5];

Memory requirement = $5*2B=10B$

 

And for

union {
float y;
long z;
}

Memory requirement = $8B$ because the size of a union is the size of it's largest data member.

Hence, total memory requirements = $18B$

Option C



Let's consider memory alignment now.

If machine architecture has word size = 32 bits, then every piece of addressable memory must be in multiples of a word. Or equivalently 32 bits. Or, equivalently 4B.

Hence for:

short x[5];

Memory requirement = $5*2B=10B$ $+2B$ of padding = $12B$ (Multiple of 4B)

for:

union {

Memory requirement = $8B$

So, total = $20B$


If machine architecture has word size = 64 bits, then every piece of addressable memory must be in multiples of a word. Or equivalently 64 bits. Or, equivalently 8B.

Hence for:

short x[5];

Memory requirement = $5*2B=10B$ $+6B$ of padding = $16B$ (Multiple of 8B)

for:

union {

Memory requirement = $8B$

So, total = $24B$

2 Comments

Thanks for explaining the second half.
1
1
Amazing😊
0
0
2 votes
2 votes
struct ( 
    short x[5];  
    union { 
        float y; 
        long z; 
    } u;
 )t;

union considers max value only.

struct (

short x[5];

8bytes; //long

)t;

struct considers every value

5*2// 5 values of short =8 =>18

0 votes
0 votes
Short[5]=5*2=10

Max[float,long]= max[4,8]=8

Total=short[5]+max[4,8]=10+8=18
Answer:

Related questions