The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+20 votes
2.6k views

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

 

asked in Programming by Veteran (68.8k points)
edited ago by | 2.6k views
what is the concept of alignment here..?
what if, we consider the alignment then what will be the answer?
thanks for sharing nitish, I am unaware of this.
So, as it is architecture dependent then it means we can not assume the padding by ourself right.
yes it is architecture dependent, but in general machines we take 32bit or 64 bit architecture,

but whenever qsn will be asked machine architecture(word size) will be explicitly mentioned...
In this question if we have to consider the alignment then answers will be like this-

for 32 bit architecture: 2*5 (+2 Byte padding)+8=10+2+8=20Byte

and for 64 bit architecure: 2*5(+6 byte padding)+8=10+6+8=24Byte
yes..
how you add 2 byte for 32 bit architecture becouse 2^k means 2^5= 32 bit and 2^6=64 bit

..if any other logic here pz explain

2 Answers

+35 votes
Best answer

answer is (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

answered by Loyal (3.3k points)
edited ago by
ISRO answer key given option C. totally wrong. anyone also not argue with this. someone loose 3 marks easily. who care about it. please download answer key  and verify it.

That's the worst key in 2015 paper- but 2-3 other ones were also wrong even after 6 were changed in the paper. If they don't care for quality they could hire from GATE only.

https://drive.google.com/file/d/0B8_aYGBndW4Hd2JNWDYxYlVpYlU/view

@Arjun Sir, Please comment if u find this wrong.

If we dont ignore address alignment, then answer is 24 ?
- Because every address has to be in form of $2^k$ for some k (except char). After 10 byes of short data type array, we have to pad 6 bytes to make offset of 16 to next starting address of long.
Which gives
10 + 6 (pading) + 8 = 24.

char can take any address, infact we do padding of characters only. This structure would become like this-

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


Is it right ?

Yes same problem . Can anyone describe it what is the alignment and if it is included how the answer will change ?

 

#include<stdio.h>
struct {
    short x[5];
    union {
        float y;
        long z;
    } u;
 }t;
int main() {
   printf("short=%d\n",sizeof(short));
     printf("float=%d\n",sizeof(float));
       printf("long=%d\n",sizeof(long));
printf("%d",sizeof(t));
 return 0;
}
ans :16

Short : 2 bytes

float: 4 bytes

double : 4 bytes

Answer should be 14 but how it is 16 ?

@Arjun Sir
Can you please tell me if your procedure is correct?
Why union creates only memory for long z??

A union is a class all of whose data members are mapped to the same address within its object. The size of an object of a union is, therefore, the size of its largest data member.

Since longz is the largest data member it will create only for it

https://en.wikipedia.org/wiki/Union_type

Thank you so much.
since the size of union is the largest data member size, can there be padding at the end?
if we consider alignment answer is 24 bytes :)
+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

 

answered by (135 points)


Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

32,505 questions
39,217 answers
109,115 comments
36,599 users