Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ANSI C unions - are they really useful?

Tags:

c

unions

From a response to some question yesterday, I learned that it is nonportable and unsafe to write into one union member and read the value from another member of a different type, assuming underlying alignment of the members. So after some research I found a written source that repeats this claim and specifies a popular example - using union of int and float to find the binary representation of a float.

So, understanding that this assumption is not safe, I wonder - except for saving memory (duh...) what real use is there to unions?

Note: that is, under Standard C. Clearly, for a specific implementation, the rules are known in advance and can be taken advantage of.

EDIT: the word "unsafe", due to association of recent years, is probably a bad choice of wording, but I think the intention in clear.

EDIT 2: Since this point repeats in the answers - saving memory is a valid argument. I wanted to know if there was something beyond that.

like image 826
ysap Avatar asked Aug 12 '10 03:08

ysap


People also ask

How are unions useful in C?

C unions allow data members which are mutually exclusive to share the same memory. This is quite important when memory is valuable, such as in embedded systems. Unions are mostly used in embedded programming where direct access to the memory is needed.

What are the limitations of union in C?

Disadvantages of union. You can use only one union member at a time. All the union variables cannot be initialized or used with varying values at a time. Union assigns one common storage space for all its members. Only one member can be accessed at a particular period of time.

For what kind of applications are unions are useful?

The primary use of a union is allowing access to a common location by different data types, for example hardware input/output access, bitfield and word sharing, or type punning. Unions can also provide low-level polymorphism.

Which is more efficient structure or union?

Union takes less memory space as compared to the structure. Only the largest size data member can be directly accessed while using a union. It is used when you want to use less (same) memory for different data members. It allocates memory size to all its data members to the size of its largest data member.


2 Answers

Yes.

The provide a way of creating generic containers. Though, to get polymorphic behavior you must implement a vtable or type switching yourself...

There are, however, one of those features that you only use when you need them and need rather rarely.

like image 137
dmckee --- ex-moderator kitten Avatar answered Oct 15 '22 04:10

dmckee --- ex-moderator kitten


Even if unions don't offer much in immediate usefulness (reduced memory usage aside), one advantage of using a union over dumping all of its members into a struct is that it makes the intended semantics clear: only one value (or set of values if it's a union of structs) is valid at any given time. It documents itself better.

The mutual exclusivity of members would be less obvious if you instead made all the union members separate members of a struct. Additionally, you'd still have the same problem of ill-defined behavior if you read a member that wasn't previously written to, but now you need to account for the application's semantics too (did it initialize all unused members to 0? did it leave them as garbage?), so in that sense, why wouldn't you use a union?

like image 35
jamesdlin Avatar answered Oct 15 '22 05:10

jamesdlin