Structure vs Union Memory

Structure vs Union Memory

Medium C Structures 34 views
Explanation Complexity

Problem Statement

Create a structure and union the same members. Do sizeof(). In the union, set one member and read the other - what do you get and why?

Input Format

A structure and a union with the same members.

Output Format

Size of structure
Size of union
Result of reading one union member after setting another.

Example

Members:

• int i

• float f
Size of structure = 8 bytes
Size of union = 4 bytes
Union read value = garbage / unexpected

Constraints

• Same members in structure and union

• One member written, another read

Concept Explanation

Structure stores all members separately.
Union shares the same memory for all members.

Step-by-Step Explanation

1.Create a struct with int i and float f.

2.Create a union with the same members.

3.Use sizeof() on structure:

• Memory = sum of all members.

4.Use sizeof() on union:

• Memory = size of the largest member only.

5.In union, assign a value to one member (e.g., i).

6.Read another member (e.g., f).

7. The read value is unexpected because:

• Both members share the same memory.

• Interpreting the same bits as a different type gives garbage.

Concept Explanation

Structure stores all members separately.
Union shares the same memory for all members.

Step-by-Step Explanation

1.Create a struct with int i and float f.

2.Create a union with the same members.

3.Use sizeof() on structure:

• Memory = sum of all members.

4.Use sizeof() on union:

• Memory = size of the largest member only.

5.In union, assign a value to one member (e.g., i).

6.Read another member (e.g., f).

7. The read value is unexpected because:

• Both members share the same memory.

• Interpreting the same bits as a different type gives garbage.

Input / Output Format

Input Format
A structure and a union with the same members.
Output Format
Size of structure
Size of union
Result of reading one union member after setting another.
Constraints
• Same members in structure and union

• One member written, another read

Examples

Input:
Members: • int i • float f
Output:
Size of structure = 8 bytes Size of union = 4 bytes Union read value = garbage / unexpected

Example Solution (Public)

C
#include <stdio.h>

// Define structure and union with same members
struct MyStruct {
    int a;
    float b;
    char c;
};

union MyUnion {
    int a;
    float b;
    char c;
};

int main() {
    struct MyStruct s;
    union MyUnion u;

    // Print sizes
    printf("Size of structure = %lu bytesn", sizeof(s));
    printf("Size of union     = %lu bytesn", sizeof(u));

    // Set one member of the union
    u.a = 100;

    // Read other members
    printf("nUnion after setting a=100:n");
    printf("u.a = %dn", u.a);
    printf("u.b = %fn", u.b);   // reading float after int
    printf("u.c = %dn", u.c);   // reading char after int

    return 0;
}

Official Solution Code

#include <stdio.h>

// Define structure and union with same members
struct MyStruct {
    int a;
    float b;
    char c;
};

union MyUnion {
    int a;
    float b;
    char c;
};

int main() {
    struct MyStruct s;
    union MyUnion u;

    // Print sizes
    printf("Size of structure = %lu bytesn", sizeof(s));
    printf("Size of union     = %lu bytesn", sizeof(u));

    // Set one member of the union
    u.a = 100;

    // Read other members
    printf("nUnion after setting a=100:n");
    printf("u.a = %dn", u.a);
    printf("u.b = %fn", u.b);   // reading float after int
    printf("u.c = %dn", u.c);   // reading char after int

    return 0;
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.