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 style and taste is never easy to define, but style tends to be centered on the social, and taste upon the individual. Style then works along axes of similarity to identify group membership, to relate to the social order; taste works within style to differentiate and construct the individual. Style speaks about social factors such as class, age, and other more flexible, less definable social formations; taste talks of the individual inflection of the social.”
—John Fiske (b. 1939)
“I find it a great and fatal difference whether I court the Muse, or the Muse courts me. That is the ugly disparity between age and youth.”
—Ralph Waldo Emerson (18031882)
“Maybe we were the blind mechanics of disaster, but you dont pin the guilt on the scientists that easily. You might as well pin it on M motherhood.... Every man who ever worked on this thing told you what would happen. The scientists signed petition after petition, but nobody listened. There was a choice. It was build the bombs and use them, or risk that the United States and the Soviet Union and the rest of us would find some way to go on living.”
—John Paxton (19111985)
“If rightly made, a boat would be a sort of amphibious animal, a creature of two elements, related by one half its structure to some swift and shapely fish, and by the other to some strong-winged and graceful bird.”
—Henry David Thoreau (18171862)