-
October 17th, 2007, 11:49 AM
#1
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...
-
October 17th, 2007, 12:23 PM
#2
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
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
-
October 17th, 2007, 01:34 PM
#3
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.
Code:
union thing
{
int i;
char c[4];
};
thing u;
u.i = 2;
cout << u.c[0] << u.c[1] << u.c[2] << u.c[3]; // illegal
Originally Posted by treuss
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 01: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
-
October 17th, 2007, 03:32 PM
#4
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.
-
October 17th, 2007, 10:30 PM
#5
Re: Difference between Structures and Union
1) You cannot write the union as
//Wont work
Code:
union u
{
int i;
string str;
};
//Will work
Code:
struct s
{
int i;
string str;
};
A union member shall not be of a class type (or array
thereof) that has a non-trivial constructor.
2) Union in C++ cannot be used as the Base class while a structure can.
-
October 18th, 2007, 02:33 AM
#6
Re: Difference between Structures and Union
Originally Posted by Graham
Which is irrelevant because reading one member having written to another is prohibited by the standard (except for one specific exemption
Interessting. What is the exemption ?
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
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
-
October 18th, 2007, 11:42 AM
#7
Re: Difference between Structures and Union
Originally Posted by treuss
Interessting. What is the exemption ?
http://www.codeguru.com/forum/showpo...59&postcount=4
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
October 18th, 2007, 04:18 PM
#8
Re: Difference between Structures and Union
There is another reason for using a union - when you want to ensure that a buffer is aligned.
Note you can do something like this:
Code:
union aligned_buffer
{
double d;
char data[ sizeof( double ) ];
};
You can then safely do this:
Code:
aligned_buffer buf;
double x = 1.5;
memcpy( buf.data, &x, sizeof( double ) );
double *py = static_cast< double * >( buf.data ); // may need reinterpret_cast
You cannot do double y = buf.d; safely, according to the standard, but I have not yet come across a compiler where it fails.
The reason you need d in the union is to ensure the other member, data, is aligned so that you can safely do the cast.
Note the memcpy does not require alignment. (Note if this is C then you obviously use a C-cast, but alignment issues apply in C as well as C++).
-
October 18th, 2007, 11:00 PM
#9
Re: Difference between Structures and Union
Originally Posted by Graham
Which is irrelevant because reading one member having written to another is prohibited by the standard (except for one specific exemption). E.g.
Code:
union thing
{
int i;
char c[4];
};
thing u;
u.i = 2;
cout << u.c[0] << u.c[1] << u.c[2] << u.c[3]; // illegal
I'm still unclear about the above...Can you please explain about the same?
Thanks in advance.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|