Union (computer Science) - Difference Between Union and Structure

Difference Between Union and Structure

A union is a class all of whose data members are mapped to the same address within its object. The size of an object of a union is, therefore, the size of its largest data member.

In a structure, all of its data members are stored in contiguous memory locations. The size of an object of a struct is, therefore, the size of the sum of all its data members.

This gain in space efficiency, while valuable in certain circumstances, comes at a great cost of safety: the program logic must ensure that it only reads the field most recently written along all possible execution paths. The exception is when unions are used for type conversion: in this case, a certain field is written and the subsequently read field is deliberately different.

An example illustrating this point is:

+-----+-----+ struct { int a; float b } gives | a | b | +-----+-----+ ^ ^ | | memory location: 150 154 | V +-----+ union { int a; float b } gives | a | | b | +-----+

Structures are used where an "object" is composed of other objects, like a point object consisting of two integers, those being the x and y coordinates:

typedef struct { int x; // x and y are separate int y; } tPoint;

Unions are typically used in situation where an object can be one of many things but only one at a time, such as a type-less storage system:

typedef enum { STR, INT } tType; typedef struct { tType typ; // typ is separate. union { int ival; // ival and sval occupy same memory. char *sval; } } tVal;

Read more about this topic:  Union (computer Science)

Famous quotes containing the words difference between, difference, union and/or structure:

    The difference between guilt and shame is very clear—in theory. We feel guilty for what we do. We feel shame for what we are. A person feels guilt because he did something wrong. A person feels shame because he is something wrong. We may feel guilty because we lied to our mother. We may feel shame because we are not the person our mother wanted us to be.
    Lewis B. Smedes, U.S. psychologist, educator. Shame and Grace: Healing the Shame We Don’t Deserve, ch. 2, Harper (1993)

    The invention of photography provided a radically new picture-making process—a process based not on synthesis but on selection. The difference was a basic one. Paintings were made—constructed from a storehouse of traditional schemes and skills and attitudes—but photographs, as the man on the street put, were taken.
    Jean Szarkowski (b. 1925)

    If the Union is now dissolved it does not prove that the experiment of popular government is a failure.... But the experiment of uniting free states and slaveholding states in one nation is, perhaps, a failure.... There probably is an “irrepressible conflict” between freedom and slavery. It may as well be admitted, and our new relations may as be formed with that as an admitted fact.
    Rutherford Birchard Hayes (1822–1893)

    A special feature of the structure of our book is the monstrous but perfectly organic part that eavesdropping plays in it.
    Vladimir Nabokov (1899–1977)