1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

What are the advantages of using Unions?

Discussion in 'C' started by aspguy, May 16, 2008.

  1. aspguy

    aspguy New Member

    Joined:
    May 2, 2005
    Messages:
    59
    Likes Received:
    1
    Trophy Points:
    0
    What are the advantages of using Unions?
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    Save memory
     
  3. atul.sharma12

    atul.sharma12 New Member

    Joined:
    May 21, 2008
    Messages:
    15
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    S/W Engg.
    Location:
    This Planet only!!!!
    Let me explain you by an example..

    struct test {
    int temp1;
    char temp2;
    } test_1;

    union test {
    int temp1;
    char temp2;
    } test_2;

    when you take the size of both then you will see the difference.
    in case of structure sizeof(test_1) = 5 (I am taking the integer size as 4 bytes)
    whereas in caes of union sizeof(test_2) = 4

    In union the memory occupied by the largest data is being reused to store the other members of the union.In this case it happen to INT so the size came 4.

    Hope it will make things more clear
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    Just checkout if this is true

    sizeof(test_1) = 5

    Because of packaging format it will be 8 ( assuming what you are assuming )
     
  5. atul.sharma12

    atul.sharma12 New Member

    Joined:
    May 21, 2008
    Messages:
    15
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    S/W Engg.
    Location:
    This Planet only!!!!
    I agree......
    And I appreciate ur immediate concern.
    Forgot the padding issue...Excatly the size will be 8 due to insertion of extra bytes by compiler.

    Thank you once again for correcting me
    and sorry for the mistake.
     
  6. docesam

    docesam New Member

    Joined:
    Apr 10, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    the amount of memory saved is so small to the extend that i would like to ask who cares about this few bytes ?

    seriously !!!
     
    Last edited by a moderator: Sep 12, 2009
  7. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    Bigger font does not speak more.
     
  8. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    The obvious example of a use for unions is a spreadsheet where a cell can contain a date, number, some text, a formula or other things depending on the design.

    Another use of unions is to convert from one data type to another.
     
  9. mayjune

    mayjune New Member

    Joined:
    Jun 14, 2009
    Messages:
    817
    Likes Received:
    33
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Pune,Delhi
    It can also be used where an application will be used where every byte used is important, like lets say if you are writting an application for a small calculator which has very less memory, now every byte here being used is very important! Hence, union will be prefered. There maybe other examples too like this one
     

Share This Page