GATE CSE 2000 | Question: 1.17, ISRO2015-79
in Programming edited by
13,670 views
42 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 edited by
13.7k views

19 Comments

what is the concept of alignment here..?
what if, we consider the alignment then what will be the answer?
3
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.
1
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...
3
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
9
yes..
0
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
0
@reena_kandhari how u have calculated size with alignment conditions. Please explain
0
why u r adding  byte padding for 32 bit ?
0

This might help in understanding Data Alignment more clearly.

10

18 Bytes

3

@reena_kandari @joshi_nitish can you please explain the padding done for 32 and 64 bit machine?

@Shaik Masthan

0
1

@reena_kandarihow you calculated padding bits for 32 bit and 64 bit architecture

0

@tusharp @anchitjindal07

Please see the video above posted by Nitesh Singh.you will understand the purpose of padding & how it's done internally.

0
edited by

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

I think answer for 32 bit architecture provided by @reena_kandari is wrong.

Quoting the rules of Structure padding and alignment from SO answer

Rules:

  • Before each individual member, there will be padding so that to make it start at an address that is divisible by its size.
    e.g on 64 bit system,int should start at address divisible by 4, and long by 8, short by 2.
  • char and char[] are special, could be any memory address, so they don't need padding before them.
  • For struct, other than the alignment need for each individual member, the size of whole struct itself will be aligned to a size divisible by size of largest individual member, by padding at end.
    e.g if struct's largest member is long then divisible by 8, int then by 4, short then by 2.

For 32 bit architecture: 2*5+6(padding)+8 = 24 Bytes (If I apply 2 bytes as padding then start address isn’t divisible by 8 which is size of long given in question).

Am I correct in saying that memory size remains same for both 32 bit and 64 bit architecture because irrespective of architecture all above rules need to be satisfied but memory cycles to access the data differs in 32 bit and 64 bit architecture?

Good read to understand Structure padding and alignment:

https://www.geeksforgeeks.org/structure-member-alignment-padding-and-data-packing/

https://stackoverflow.com/questions/39527606/what-is-difference-between-structure-padding-in-c-on-32bit-and-64bit-architectur

https://stackoverflow.com/questions/4306186/structure-padding-and-packing

http://www.catb.org/esr/structure-packing/ or archive

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

4 Answers

55 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

15 Comments

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.
1

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

4

@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 ?

25
edited by
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
0
Can you please tell me if your procedure is correct?
0
Why union creates only memory for long z??
0

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

7
Thank you so much.
0
since the size of union is the largest data member size, can there be padding at the end?
0
if we consider alignment answer is 24 bytes :)
1

Structure alignment is implementation-defined. So answer after considering alignment doesn't necessarily need to be 24. Please see "IMPORTANT NOTE" and Comment section of the selected answer:

https://stackoverflow.com/questions/119123/why-isnt-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member

0
@sachin where hav u studied that address alignment part ?? i am not getting it ..  In union , the size of the largest member of the union is the size of the union ... as far i know ..
0
1
Sir, why *UNION* only conside the max?
0
I too agree with you.
0
14 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$

1 comment

Thanks for explaining the second half.
0
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
Short[5]=5*2=10

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

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

Related questions