Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unions versus structures in C

Tags:

c

unions

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?

like image 777
AGeek Avatar asked Apr 07 '09 05:04

AGeek


People also ask

What is difference between structure and union in C?

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.

Why do we use union instead of structure in C?

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.

What are the advantages of union over structure in C?

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.

What is union vs struct?

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.


2 Answers

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.

like image 189
Smashery Avatar answered Sep 20 '22 06:09

Smashery


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.

like image 23
greyfade Avatar answered Sep 18 '22 06:09

greyfade