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 the actual and the ideal force of man is happily figured in by the schoolmen, in saying, that the knowledge of man is an evening knowledge, vespertina cognitio, but that of God is a morning knowledge, matutina cognitio.”
    —Ralph Waldo Emerson (1803–1882)

    “Man is the only animal that laughs or weeps; for he is the only animal that is struck with the difference between what things are, and what they ought to be. We weep at what thwarts or exceeds our desires in serious matters; we laugh at what only disappoints our expectations in trifles.”
    —William Hazlitt (1778–1830)

    “We are constantly thinking of the great war ... which saved the Union ... but it was a war that did a great deal more than that. It created in this country what had never existed before—a national consciousness. It was not the salvation of the Union, it was the rebirth of the Union.”
    —Woodrow Wilson (1856–1924)

    “The philosopher believes that the value of his philosophy lies in its totality, in its structure: posterity discovers it in the stones with which he built and with which other structures are subsequently built that are frequently better—and so, in the fact that that structure can be demolished and yet still possess value as material.”
    —Friedrich Nietzsche (1844–1900)