If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Difference between Structures and Union

I dont want the normal ordinary answer for this question like it differes only in memory allocation and all.....
I need ..like where we use Union and where can structure.... can any one please help me in this regards...

Re: Difference between Structures and Union

You use unions only in two pretty rare cases:

1. You want the members to affect each other directly, i.e. changing the value of one member will always affect the value of another member (due to the fact that they are referring to the same memory).

2. You have two data members, and for each object you will always only use one of them. Using a union will in this case save memory, as the memory of the object will not need to be big enough for both members.

More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

Re: Difference between Structures and Union

Originally Posted by treuss

You use unions only in two pretty rare cases:

1. You want the members to affect each other directly, i.e. changing the value of one member will always affect the value of another member (due to the fact that they are referring to the same memory).

Which is irrelevant because reading one member having written to another is prohibited by the standard (except for one specific exemption). E.g.

2. You have two data members, and for each object you will always only use one of them. Using a union will in this case save memory, as the memory of the object will not need to be big enough for both members.

So, only one case, then.

Last edited by Graham; October 17th, 2007 at 02:38 PM.

Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.-- Sutter and Alexandrescu, C++ Coding Standards

Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman

The cheapest, fastest and most reliable components of a computer system are those that aren't there. -- Gordon Bell

Re: Difference between Structures and Union

Unions are used mostly in low-level C code for the two reasons mentioned by treuss. In C++ they're not used much because they're mostly incompatible with constructors and destructors. The members of a union may not have them (although the union as a whole can). So they don't work very well for creating self-managing objects with RAII and such. Unions are really only good as "passive" data structures. A C++ union doesn't strictly have to be POD, but it's pretty much all they're good for anyway.
structs in C++ are practically the same as classes, the only difference being the default access control. Since classes were added when structs already existed, I guess you can think of "class" as syntactic sugar for struct.