MCQ IN COMPUTER SCIENCE & ENGINEERING

COMPUTER SCIENCE AND ENGINEERING

DATA STRUCTURES

Question [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
Consider the following C declaration:struct { short s[5]; union { float y; long z; }u; } t;Assume that objects of the type short, float and long occupy 2 bytes, 4 bytes and 8 bytes, respectively. The memory requirement for variable t is . . . . . .
A
22 bytes
B
10 bytes
C
14 bytes
D
18 bytes
Explanation: 

Detailed explanation-1: -Since u is a union, memory allocated to u will be max of float y(4 bytes) and long z(8 bytes). So, total size will be 18 bytes (10 + 8).

Detailed explanation-2: -The total memory required to store a structure variable is equal to the sum of size of all the members.

Detailed explanation-3: -However, the size of uJob is 32 bytes. It’s because the size of a union variable will always be the size of its largest element. In the above example, the size of its largest element, ( name[32] ), is 32 bytes. With a union, all members share the same memory.

Detailed explanation-4: -Succinctly, the size of a union is the size of its largest element; the size of a structure is the sum of the sizes of its elements, plus padding where needed.

There is 1 question to complete.