In C/C++, Structures and Union are two user-defined data types. In this blog post, we will try to understand how they both work and how exactly are they different from each other.
What is Structure? (struct)
Struct is a user-defined data-type that’s used to store a combination of data which can potentially belong to different data-types. To explain further, think about how you can store information about a student in C/C++, where, you need to store the following parameters:
- Name
- Class
- Phone Number
- Email Address
One way to go about it is to store it in 4 different arrays: name[], class[], phone[], email[]
. Basically, name[i]
will represent the name of the ith student. class[i]
will represent the class of the corresponding student. phone[i]
will represent the phone number and email[i]
will represent the email address of ith student.
The advantage of this method is that it is the easiest one to think of. The downside is that it is quite difficult to manage a student this way. Here, we have just four parameters. Imagine a situation where we have 400 parameters associated with a student. One simply cannot manage 400 arrays. This is where structs come into the picture.
Defining a Structure
Through struct, we can define the structure of a single student as follows:
struct Student { string name; int class; string phone; string email; }
We can then simply define an array of students as: Student students[10]
.
The above structure beautifully captures the details of a student. Here are some of the advantages of this implementation:
- All the variables of a particular student are packaged together and it is much cleaner.
- The parameters of a particular student are all stored sequentially in the computer memory. This leads to far more efficient caching of student data.
What is Union?
Suppose we are conducting a survey of various people living in our society. As a part of this survey, our aim is to try to record the height (in mm) and weight (in kgs) of various people. The height could be something like 1700mm. Weight could be 74.23 kgs. Height is always an integral value (in mm) while weight could be fractional. The issue here is that for some people we have only height data. For others, we have only weight data. The obvious way to store such data is to create a struct.
struct Person { int height; double weight; }
The issue here is that we only have either the height or the weight available for each person. However, we are allocating space for both. This leads to a wastage of memory. What if we could store only height for those whose height is the available and only weight for people where we have the weight value at our disposal? It would make things much simpler and also save memory. This is where unions help.
union Person { int height; double weight; }
Now, we can easily store the details of a Person as follows:
union Person person1 = ; union Person person2 = {.weight=74.23};
When we create the person1 object, space is allocated only for height. No space is allocated for weight. Similarly, when we create the person2 object, space is allocated only for weight. No space is allocated for height. Here, depending on the architecture, we are saving 4 - 8 bytes on each of the first as well as the second object. Imagine if we had data for a billion people, how much bytes we would be able to save?
Let us take a look at code
The output for this code is as follows:
Observe here that in the first case, the size of person1_union and person2_union is each 8 bytes. This is because a Union needs to store the size of the largest data type as a part of it. For instance, if a union stores a character and a double value, its size will be that of the double value because even if it is storing a character, there should be space allocation for double value.
The great thing here is the amount of size savings that we have achieved - literally half the size as that of the struct. Imagine if we are storing such data for each person on earth - we will save several gigabytes of storage space/cost.
Difference between Structure and Union
Let us summarize our understanding of Union and Struct in the form of a table to highlight the differences between structure and union:
Struct | Union |
---|---|
Used for storing various data types that when aggregated, represent a user-defined data type. | Used for storing one of many data types that are available. |
It occupies space for each of the inner parameters. | Occupies space equivalent to the parameter with the highest size. |
All members store some value at any point in time. | Exactly one member stores a value at any particular instance. |
Mastering Data Structures & Algorithms using C and C++
Conclusion
This is all about the main difference between structure vs union. These two are the type of data structure. These two are the user-defined data types that contain a variable of different data types.
People are also reading: