Difference between Structures and Union
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Difference between Structures and Union

  1. #1
    Join Date
    Sep 2006
    Posts
    8

    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...

  2. #2
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    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.

  3. #3
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,471

    Re: Difference between Structures and Union

    Quote 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
    Quote 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


  4. #4
    Join Date
    Dec 2005
    Posts
    642

    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.

  5. #5
    Join Date
    May 2007
    Posts
    85

    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.

  6. #6
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Difference between Structures and Union

    Quote 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.

  7. #7
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

  8. #8
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    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++).

  9. #9
    Join Date
    May 2007
    Posts
    85

    Re: Difference between Structures and Union

    Quote 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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center