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!)

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

    • Brian
    • Posted August 7, 2023 at 7:56 pm
    • Permalink

    I found a verrry practical use for a union: to communicate a struct, having assorted fields (total size 480 bytes) that exists on MachineA ——-> to MachineB, over dedicated Ethernet. MachineB runs software written in a different language, but the same struct defined, and the only communication tool they share is Sockets (which only handles a simple array of byte or char).
    The solution? On each Machine, create a union combining an array of char[480] with the same struct totaling 480 bytes (sharing the same memory).
    Now on MachineA, populate the fields in the struct. Call Sockets to send the array of char[480] to MachineB.
    Due to the nature of a union, the struct fields on MachineB will now magically match the struct fields on MachineA, though there was no way to communicate them directly. Useful for data acquisition applications.
    I hope this is helpful to someone.


One Trackback/Pingback

  1. By UNION IN C :- – TechTalk on 24 Feb 2018 at 10:17 am

    […] Difference between struct and union in C++ […]

Leave a comment