1,321 views

Which of the following statements is correct?

• S1: A struct object will always occupy more space than an union object having the same elements.
• S2: Given an int array Arr and a struct object Str both having same size in memory, Arr[100] and Str[100] always have the same size in memory.
1. Only S1 is correct
2. Only S2 is correct
3. Both S1 and S2 are correct
4. Neither S1 nor S2 is correct

If struct object contains int a and char b,

Struct str
{

int a;

char b;

} str[100];

and int arr[100]

then will both str and arr have same memory ??
That is not the question statement rt?

sir, but why can't we take this example here ... I know it is not mentioned here explicitly but we know that struct object may contain more than one variable, so I thought it may not be possible always ..

No, the question statement says that the struct object nd the array object have same memory size. This was explicitly stated so that structure padding is taken care of. Now, whatever be the object, when an array is made size simply gets multiplied.
but in the question statement it is cleary given that, both arr and str object has same size in memory,,,so when you crate arr[100] ,, it will be actually 2d array which has always same size of str[100].
ohhh  got it ..my bad. . thank you @arjun sir and @subham sir :)
why statement 1 is false??

S1: A struct object will always occupy more space than a union object having the same elements.

Struct str
{

int a;

char b;

Size = size of int a + size of char b. = 4 + 1 = 5.

Union U
{

int a;

char b;

size =  max (size of int a , sizeof char b).= size of int a = 4.

@vijaycs but i didn't get second statement.
^ Union U u1 ;

Now what should be the size of u1 ?

= size of an int.   // here we are not including the size of char variable. Because in union both the variables use the same memory.( and memory size = size of the maximum size variable.and here its int not char. But it is not so in the case of structure variable.

= 4.

I meant second statement of question  Given an int array Arr and a struct object Str both having same size in memory, Arr[100] and Str[100] always have the same size in memory.

Given an int array Arr and a struct object Str both having same size in memory, Arr[100] and Str[100] always have the same size in memory.

if both objects have the same size of memory then 100 objects of both will also have the same memory.

Does this mean that size of  one element of arr array and one struct object is same.

" Given an int array Arr and a struct object Str both having the same size in memory "
^Yes

" Given an int array Arr "  refers to one element of an array and not the entire array  . That is ambiguous.

Something is not right.

No, arr is an array - not array element.

This question is supposed to involve many concepts.

• In C both struct and union are used for collection of objects. But the difference is, in union we are supposed to use only one of them at a time. i.e., suppose we have 2 data objects - say int and a char, then it allocates memory only for int, and uses the same for char. So, at any time, we cannot use both the items but only one.

Now, coming to S1, it is FALSE. Both struct and union will be having same size for a single object. Only with multiple objects, struct will have more size.

For S2, it is true. Because an array - be of any object- will just multiply the memory requirement of a single object. This statement is not asking for structure padding, which happens inside a single structure object. Here arr is an array and hence arr[100] is an array of arrays.

In structure padding, size of a single struct object is increased by adding padding bits to ensure proper alignment with the architecture causing an increase in size compared to the sum of sizes of the individual components.

by

sir, in stmt one, the last word is 'elements' not element so we should not consider the case of single element. so it should be true??
That's too small a reason. Element's' is used to give the possibility of multiple elements. Don't think using this we can exclude 'single element'.
sir  "In union we are supposed to use only one of them at a time " is it true that one of them should be anyone bt space given to union should be max of (int or char ) always ??            //a/c to aboove example
yes. And that is why space taken is maximum of all.

Size of a struct = Sum of the sizes of all constituent members.

Size of a union = Size of the largest member.

If struct and union have the one, same member, then size of struct = size of union.

S1 is false.

If size of the struct Str = x, and size of array Arr = x; then size of Str[[100] = 100x, and size of Arr[100] = 100x.

S2 is true.

Option B