Skip navigation

What’s the difference between a STRUCT and a UNION in C++?

Here’s an example!

A C++ struct will make sure to allocate space for each and every member in the struct.

For instance, if we had


struct VertexStruct
{
    float x,y,z;
};

When you create one, using a line of code like:

VertexStruct vertexstruct;

This is what VertexStruct object will look like in memory.


     vertexstruct
 ___________________
 |     |     |     |
 |     |     |     |
 |_____|_____|_____|
    x     y     z

So if we do


vertexstruct.x = 50;
vertexstruct.y = 10;
vertexstruct.z = 2;

We get this picture in memory:


     vertexstruct
 ___________________
 |     |     |     |
 | 50  | 10  |  2  |
 |_____|_____|_____|
    x     y     z

This is natural and completely what you’d expect.

Contrast STRUCT vs UNION

Now, CONTRAST THAT with a UNION. A UNION uses the SAME EXACT PIECE OF MEMORY for __ALL__ of the members inside of it.

This is kind of counter-intuitive and weird.

An example:


union VertexUnion
{
    float x,y,z;
};

When you create one, using a line of code like:

VertexUnion vertexunion;

You get this picture in memory:


 vertexunion
 _______
 |     |
 |     |
 |_____|
    x    
    y
    z

The above union effectively provides 3 ways by which to access that same single piece of memory (x, y and z).

So if you wrote code like:


vertexunion.x = 10;
vertexunion.y = 2;
vertexunion.z = 5;

You get this picture in memory:


 vertexunion
 _______
 |     |
 |  5  |
 |_____|
    x    
    y
    z

(Last x, y and z all refer to the same piece of memory. Since the last value assigned there is 5, ALL OF x, y and z have the exact same value).

WEIRD!

* Note that the sizeof( any_union ) ends up being the sizeof( largest_member_in_union ).

Download the Visual Studio 2005 project files quick demo.

Demo is hosted by esnips! (they’re awesome!)

About these ads

13 Comments

  1. send important key tip to develop the skill in language c++

    • Tyler
    • Posted November 18, 2008 at 5:18 pm
    • Permalink

    like to say this is a simple fantastic way to explain the difference …bravo sir

    • radhika
    • Posted September 14, 2009 at 1:10 pm
    • Permalink

    bakwaasssssssssssssss……………..no useeeeeeeeeeeeeee

    • jithesh.p
    • Posted February 1, 2010 at 8:59 am
    • Permalink

    1. The way structure occupies memory for its member is different from union.
    (a) Structure occupies appropriate separate memory for its members
    (b) Union occupies memory for that member which needs largest chunk of bytes.
    2. We can initialize any of the structure members while initializing any of the union member other than the first member may have unpredictable results.

  2. I am glad I found your blog on yahoo. Thanks for the sensible critique. Me and my sister were just preparing to do some research about this. I am very glad to see such reliable info being shared freely out there.
    Best wishes,
    Francis from Erie city

    • makwana mehul r.
    • Posted April 22, 2010 at 2:09 pm
    • Permalink

    send me by all about c,c++,c#,java,j2ee
    visual basic
    all about the language

    • meghna
    • Posted November 14, 2010 at 3:50 pm
    • Permalink

    not properly undestood

    • Bagesh kumar bagi
    • Posted January 5, 2011 at 3:23 pm
    • Permalink

    how to represent the union in memory…

    • Anonymous
    • Posted November 17, 2011 at 8:36 am
    • Permalink

    really like that, a strange way to explain

    • Anonymous
    • Posted December 9, 2011 at 5:16 am
    • Permalink

    thanks boss

    • Rafis
    • Posted December 16, 2011 at 3:44 am
    • Permalink

    So, when we use struct or union?

  3. A nice, simple explanation. I’ve been using struct and union for a while without really understanding the difference.
    Had you thought about using an example for union that shows an easy way to convert 8 bit variables into 16, or 32 bit.

    typedef union
    {
    unsigned char data_8[2];
    long data_16;

    } conv8to16 ;

    conv8to16 wh16 ; //assign

    wh16.data_8[0]=0x27;
    wh16.data_8[1]=0x45;
    wh16.data_16 now contains 0x4527

  4. It amazes me again and again how a professional textbook can fail to explain in 3 pages what you’ve conveyed to me in a one page blog post. Thank you.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 42 other followers

%d bloggers like this: