Union Memory Sharing

Union Memory Sharing

Hard C Variables 35 views
Explanation Complexity

Problem Statement

Create a union containing an int, float, or char array. Store a value in one member, then read it from the other. What value will be returned, and why (concept of memory overlap)?

Input Format

A union with members:

• int i

• float f

• char arr[]

Output Format

Value read from a different union member after writing one member.

Example

Store value in int i = 65
Read value from char arr[0]
A (or unexpected value)

Constraints

• All members share the same memory

• A union uses one shared memory block for all members.Only one member should be used at a time

Concept Explanation

A union uses one shared memory block for all members.

Step-by-Step Explanation

1.Create a union with int, float, and char array.

2.Store a value in one member (for example, int i = 65).

3.Read another member (for example, char arr[0]).

4.The value read is not converted.

5.The same memory bits are reinterpreted as another type.

6.Because memory overlaps, the result can be unexpected or garbage.

Concept Explanation

A union uses one shared memory block for all members.

Step-by-Step Explanation

1.Create a union with int, float, and char array.

2.Store a value in one member (for example, int i = 65).

3.Read another member (for example, char arr[0]).

4.The value read is not converted.

5.The same memory bits are reinterpreted as another type.

6.Because memory overlaps, the result can be unexpected or garbage.

Input / Output Format

Input Format
A union with members:

• int i

• float f

• char arr[]
Output Format
Value read from a different union member after writing one member.
Constraints
• All members share the same memory

• A union uses one shared memory block for all members.Only one member should be used at a time

Examples

Input:
Store value in int i = 65 Read value from char arr[0]
Output:
A (or unexpected value)

Example Solution (Public)

C
#include <stdio.h>

union Data {
    int i;
    float f;
    char str[4];
};

int main() {
    union Data data;

    /* Store int value */
    data.i = 65;

    printf("After storing int:n");
    printf("data.i   = %dn", data.i);
    printf("data.f   = %fn", data.f);
    printf("data.str = %snn", data.str);

    /* Store float value */
    data.f = 65.5;

    printf("After storing float:n");
    printf("data.i   = %dn", data.i);
    printf("data.f   = %fn", data.f);
    printf("data.str = %snn", data.str);

    /* Store string */
    data.str[0] = 'A';
    data.str[1] = 'B';
    data.str[2] = 'C';
    data.str[3] = '�';

    printf("After storing char array:n");
    printf("data.i   = %dn", data.i);
    printf("data.f   = %fn", data.f);
    printf("data.str = %sn", data.str);

    return 0;
}

Official Solution Code

#include <stdio.h>

union Data {
    int i;
    float f;
    char str[4];
};

int main() {
    union Data data;

    /* Store int value */
    data.i = 65;

    printf("After storing int:n");
    printf("data.i   = %dn", data.i);
    printf("data.f   = %fn", data.f);
    printf("data.str = %snn", data.str);

    /* Store float value */
    data.f = 65.5;

    printf("After storing float:n");
    printf("data.i   = %dn", data.i);
    printf("data.f   = %fn", data.f);
    printf("data.str = %snn", data.str);

    /* Store string */
    data.str[0] = 'A';
    data.str[1] = 'B';
    data.str[2] = 'C';
    data.str[3] = '�';

    printf("After storing char array:n");
    printf("data.i   = %dn", data.i);
    printf("data.f   = %fn", data.f);
    printf("data.str = %sn", data.str);

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

                                        
Please login to submit solutions.