answersLogoWhite

0


Best Answer

There are no disadvantages as such. This is really no different to choosing between using a float or an integral type. Each is intended for a specific purpose so there's really no point looking for disadvantages of one over the other. It's far better to simply look at the purpose for which each was intended and decide which to use according to your needs.

A structure is an aggregate of two or more members where every member has its own address and may be accessed independently of the other members (changing the value of one member does not affect any other members). The members of a union, however, share the same memory address and thus only one value may be stored in a union at any given moment.

The size of a union is equal to the largest member of that union, plus any padding bytes required to bring the total size up to the nearest word boundary (typically a 4-byte boundary on a 32-bit machine). The size of a structure, however, is equal to the sum of its member sizes plus any padding bytes require to align the individual members as well as the structure itself. In memory-restricted systems, it is best to declare members in order of size, largest first, to conserve memory.

Basically, you use a structure as you would a database record, to group related data elements as a single entity, such as a person's name, their address, telephone number and other contact details when creating a list of contacts.

You use a union when you need to be able to store two or more different types of information but only require access to one specific type at a time. For instance, if a piece of data can either be referred to by name or by number, but never both, a union makes sense as you don't waste memory allocating storage for two separate pieces of data when you only actually require one value in memory at any given moment.

Note that the relationship between the data stored in a union and the member that was used to write that data purely conventional. It is entirely up to the programmer to keep track of which member was last used to write the data and thus read back that data correctly. Although it is an error to write through one member and subsequently read through another, it is not illegal. However, such code should be treated with suspicion:

"Unions are often misused for 'type conversion'. This misuse is practiced mainly by programmers trained in languages that do not have explicit type conversion facilities, so that cheating is necessary. For example, the following "converts" an int to int* simply by assuming bitwise equivalence:

union Fudge {

int i;

int* p;

};

int* cheat (int i) {

Fudge a;

a.i = i;

return a.p; // bad use

}

"This is not really a conversion at all. On some machines, an int and an int* do not occupy the same amount of space, while on others, no integer can have an odd address. Such use of a union is dangerous and non-portable." [Bjarne Stroustrup, The C++ Programming Language, Fourth Edition].

In order to use a union correctly, it is often necessary to use a "type field" to keep track of the active member. Both the type field and the union it relates to are typically declared as members of the same structure, thus making the relationship between the data and its type more concrete. For instance, if a piece of data can be represented by a string or by an integer (a name or a number), we might use the following enum, union and structure:

typedef enum type_field {name, number};

typedef union my_union_t {

char* pstr;

int num;

} my_union;

typedef struct my_struct_t {

type_field type;

my_union data;

} my_struct;

Whenever we need to write to the my_struct.data member, we first change the my_struct.type member to the appropriate type (name or number), and then write to my_struct.data.pstr or my_struct.data.int as appropriate. when we wish to read back the data, we test the my_struct.type member and then read back the appropriate member of my_struct.data.

Note that although it may seem we are using just a much memory as we would with a structure alone, by virtue of the use a type field, we'd still have to use a type field if we used a structure instead of a union because we'd still need to know which member was "active", even if those members did not share a memory address.

Although unions can be essential for compactness of data and performance, most programs don't improve much from the use of unions and unions are somewhat error-prone due to the onus being on the programmer to ensure data is read correctly. In C++, many of the advantages of a union can also be rendered moot by the used of derived classes, however we can also encapsulate a union within a class and thus shift the onus of responsibility to the class itself, thereby ensuring data is always interpreted correctly.

User Avatar

Wiki User

8y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are the disadvantages of unions over structures?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Usage of unions?

Protection from the greed and evil of the company. Answer #2: Given that memory is no more at premium, I really do not see any practical reason to prefer unions over structures.


What are the advantages and disadvantages of wokers being in a trade union?

Unions provide strength. Unions provide support. Unions protect.


What is the index of structures and unions in C programming?

There is no such thing. Structs and unions have fields, which can be accessed by their names.


What are the Advantages and disadvantages of production systems?

disadvantages of Production Structures


What are the advantages and disadvantages in steel structures?

whatare advatages and disadvantages of steel


What are structures and functions?

product orientated disadvantages


What are the different organization structures recommended for project organization and what are their advantages and disadvantages?

What are the different organization structures recommended for project organization? Discuss their advantages and disadvantages


What are the disadvantages of credit unions?

If you withdraw early from a term deposit, you are charged a large fee


What are the disadvantages of human labor?

Unions, benefits, slacking off, unreliable, to name a few items.


How many credit unions in Ireland?

There are over 500 credit unions in Ireland, that are members of the Irish League of Credit Unions.


Similarities between union structure enum in c?

Structures and unions share the following characteristics: * Their members can be objects of any type, including other structures and unions or arrays. A member can also consist of a bit field. * The only operators valid for use with entire structures and unions are the simple assignment (=) and sizeof operators. In particular, structures and unions cannot appear as operands of the equality ( == ), inequality (!=), or cast operators. The two structures or unions in the assignment must have the same members and member types. * A structure or a union can be passed by value to functions and returned by value by functions. The argument must have the same type as the function parameter. A structure or union is passed by value just like a scalar variable; that is, the entire structure or union is copied into the corresponding parameter. similarity is all three are user defined data types.


Why structure variables cannot be compared?

They can be compared with memcmp, but you should be careful if your structures contain:- pointers- alignment gaps- numeric variables (byte order!)- nested structures/unions