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 tragedy and comedy is the difference between experience and intuition. In the experience we strive against every condition of our animal life: against death, against the frustration of ambition, against the instability of human love. In the intuition we trust the arduous eccentricities we’re born to, and see the oddness of a creature who has never got acclimatized to being created.”
    —Christopher Fry (b. 1907)

    “To be honest, I knew that there was no difference between dying at their years old and dying at seventy because, naturally, in both cases, other men and women will live on, for thousands of years at that.... It was still I who was dying, whether it was today or twenty years from now.”
    —Albert Camus (1913–1960)

    “Some are petitioning the State to dissolve the Union, to disregard the requisitions of the President. Why do they not dissolve it themselves,—the union between themselves and the State,—and refuse to pay their quota into its treasury? Do not they stand in the same relation to the State that the State does to the Union? And have not the same reasons prevented the State from resisting the Union which have prevented them from resisting the State?”
    —Henry David Thoreau (1817–1862)

    “Science is intimately integrated with the whole social structure and cultural tradition. They mutually support one other—only in certain types of society can science flourish, and conversely without a continuous and healthy development and application of science such a society cannot function properly.”
    —Talcott Parsons (1902–1979)