The idea behind this question is to understand the deeper concepts of using union and using it in different way so as to save memory.. My question to all is--
let's say there is a structure
struct strt
{
float f;
char c;
int a;
}
and the same structure represented in union
union unin
{
float f;
char c;
int a;
}
If I allocate values to structure members one after another and then print them, it gets printed. But in case of union it doesn't happen, some overwriting is being done..
So I need to find out a method which can store the values of f,c,a using union and then I can print the same. (Apply any operations or anything..) but I am in search of this technique.. Can anybody out there guide me or give me any idea?
A structure is a custom data type that holds multiple members of different data type under a single unit where union is a user defined data type that combine object of different data type in the exact memory location.
Union in C is a special data type available in C that allows storing different data types in the same memory location. You can define a union with many members, but only one member can contain a value at any given time. Unions provide an efficient way of using the same memory location for multiple purposes.
Advantages of unionIt occupies less memory compared to the structure. When you use union, only the last variable can be directly accessed. Union is used when you have to use the same memory location for two or more data members. It enables you to hold data of only one data member.
A structure contains an ordered group of data objects. Unlike the elements of an array, the data objects within a structure can have varied data types. Each data object in a structure is a member or field. A union is an object similar to a structure except that all of its members start at the same location in memory.
If you were to look at how a struct stores its values, it would be something like this:
|0---1---2---3---|4---|5---6---7---8---|
|ffffffffffffffff| | | <- f: Where your float is stored
| |cccc| | <- c: Where your char is stored
| | |aaaaaaaaaaaaaaaa| <- a: Where your int is stored
So when you change the value of f, you are actually changing bytes 0-3. When you change your char, you are actually changing byte 4. When you change your int, you are actually changing bytes 5-8.
If you now look at how a union stores its values, it would be something like this:
|0---1---2---3---|
|ffffffffffffffff| <- f: where your float is stored
|cccc------------| <- c: where your char is stored
|aaaaaaaaaaaaaaaa| <- a: where your int is stored
So now, when I change the value of f, I am changing bytes 0-3. Since c is stored in byte 0, when you change f, you also change c and a! When you change c, you're changing part of f and a - and when you change a, you're changing c and f. That's where your "overwriting" is happening. When you pack the 3 values into the one memory address, you're not "saving space" at all; you're just creating 3 different ways of looking at and changing the same data. You don't really have an int, a float, and a char in that union - at the physical level, you've just got 32 bits, which could be viewed as an int, a float, or a char. Changing one is meant to change the others. If you don't want them to change each other, then use a struct.
This is why gcc tells you that your struct is 9 bytes long, while your union is only 4 - it's not saving space - it's just that structs and unions are not the same thing.
I think you misunderstand the purpose of a union
.
A union
, as the name suggests, defines a structure where all of its members occupy the same memory space. Whereas a struct
places each of its members in separate memory in a single, contiguous area.
With your union, when you write:
union foo;
foo.c = 3;
Then foo.a
and foo.f
will both be changed. This is because .a
, .c
, and .f
are stored at the same memory location. Thus, each member of a union is a different "view" of the same memory. This does not happen with a struct
because all of the members are distinct and separate from each other.
There is no way around this behavior because it's intentional.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With