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:

    I see less difference between a city and a swamp than formerly.
    Henry David Thoreau (1817–1862)

    The real grounds of difference upon important political questions no longer correspond with party lines.... Politics is no longer the topic of this country. Its important questions are settled... Great minds hereafter are to be employed on other matters.... Government no longer has its ancient importance.... The people’s progress, progress of every sort, no longer depends on government. But enough of politics. Henceforth I am out more than ever.
    Rutherford Birchard Hayes (1822–1893)

    The sacred obligation to the Union soldiers must not—will not be forgotten nor neglected.... But those who fought against the Nation cannot and do not look to it for relief.... Confederate soldiers and their descendants are to share with us and our descendants the destiny of America. Whatever, therefore, we their fellow citizens can do to remove burdens from their shoulders and to brighten their lives is surely in the pathway of humanity and patriotism.
    Rutherford Birchard Hayes (1822–1893)

    Vashtar: So it’s finished. A structure to house one man and the greatest treasure of all time.
    Senta: And a structure that will last for all time.
    Vashtar: Only history will tell that.
    Senta: Sire, will he not be remembered?
    Vashtar: Yes, he’ll be remembered. The pyramid’ll keep his memory alive. In that he built better than he knew.
    William Faulkner (1897–1962)