Any language is defined by the attributes it holds, and the properties it posses.  What makes it better than other of the same type, is how better its efficiency is and how comfortable it make things for its users.

No matter whichever programming language we talk about, C, C++ etc, all of these have their own ways to deal with the code and organise it best for the programmer-ease.

Among many of these elements, are Structure and Union. As soon as you read these words, the first thing that clicked your mind would be the fact that they are more or less the same thing in functioning. But, Is it really so? Well, the answer is a big NO.

As deeper as you will dig about these two terms, you will realize that they have considerable differences and work differently while being registered as to be same by people. This article will help you understand the Difference between structure and union by highlighting the working of both of them and drawing out the differences.

 

STRUCTURE

What is a Structure?

A Structure is called as a container of heterogeneous members. In simple words, A structure helps a programmer to keep different types of data, i.e. the data having different data-types such as integer, character, floating point etc at one place altogether. The keyword “Struct” is used to define a structure.

For example, in a C program the following code will work for structure defining-

Difference between structure and union

MEMORY ALLOCATION IN STRUCTURE

As soon as the structure is defined, the memory allocation of the members is done in contagious form. This means that the total size allocated in the memory will be at least equal to the sum of individual data-types of the members declared.  Also, it clarifies all the declared members will get there separated memory, not a shared one.

 This also means that a structure can store multiple and distinct values for different members defined in the structure. Since all the members have assigned different memory simultaneously, all the members are independent of each other for accessing them.  Also, change in value of one of the member will give no effect to that of others.

For instance, memory allocation for this code will be-

Difference between structure and union memory

Here, integer takes 1byte, character takes 2 bytes and float takes 4 bytes, so the total memory allocated for e1 object comes out to be at least=7 bytes. It can be more than this since integer sometimes takes 2 bytes or 4bytes also, but couldn’t go down this.

Here’s how memory is allocated separately to all the members-

Difference between structure and union

 

UNION

What is a Union?

A Union, as said and heard, is also a tool which is used to store altogether, the different type of data in one place for ease in working. Even though it provides the same facility, its working is quite different. Firstly, we use “Union” keyword to define a union in a program.

SYNTAX example for a Union declaration is-

Difference between structure and union

The syntax is pretty much same as that of defining a structure, except the keyword used. However, the major difference between both lies in the way they allocate memory. So, let’s see the memory allocation in Union.

MEMORY ALLOCATION IN UNION

Memory allocation in Union is totally different from that of in structure. Unlike Structure, it doesn’t gives separate memory to all the variables defined in the union. Rather, it uses the concept of “Shared memory” wherein it allocates the memory which is equals to the size of the highest data-type in the ones declared, and then uses the same memory for all of them.

For instance, taking the same piece of code as in structure, here,

Difference between structure and union

The memory allocated by the union will be 4 bytes in total which is equals to the highest of the data-type float. It will not give separate memory to the variables like structure. This is somewhat how the shared memory is allocated in a union-

Difference between structure and union

However, other things to note down about a union is that it can allow multiple forms, but only one can be accessed at a time. Thus, it allows only single values for different data-types. Also, the editing in data values of one might bring a change in other too since they reside in the same memory palace.

ADVANTAGE of structure over union:

Structures are preferred over union for the codes where all the declared variables are needed to be accessed simultaneously, and their values are to be edited time to time. In such cases, the independency provided by structure is really helpful, which cannot be attained using unions.

ADVANTAGE of union over structure:

It requires less RAM space, hence, its faster and more efficient in working as compared to its opposition. It is preferred over structure in places where one form of data is required at a time, since it increases the speed of working.

So far, this is all one needs to know to fairly differentiate between the two. On the basis of all that is found, a summarize difference table can be drawn between the two as-

BASIS

PROPERTY OF STRUCTURE

PROPERTY OF UNION

1.KEYWORD

Keyword used here is “struct”.

Keyword used is “Union”.

2.MEMORY ALLOCATION

Memory allocation is done separately for separate variables i.e in contagious form.

Memory is allocated same to all the variables i.e shared memory concept is used.

3.MEMORY SIZE

Memory size is equals to the sum of the data-type’s individual memory need or greater than that.

Total Memory allocated here is equals to the memory required by the highest data-type variable and is shared then.

4.VARIABLE ACCESS

Different members of the structure can be accessed at the same time.

Only one member can be accessed at a time.

5. VALUES

It can deal with multiple and distinct values without any issue.

Can only deal with a single unique value at one time.

6. IMPORTANCE

It is highly used where multiple variables are required to work with, for better independency.

It is preferred in places where only single variable is involved but faster working is required.

7.MEMORY EFFICIENCY

Less memory efficient.

More memory efficient.

Advertisements