Difference between Unions and Structures...

Discussion in 'C Programming' started by Ravikiran, Nov 3, 2008.

  1. Ravikiran

    Ravikiran Guest

    Hi,

    I want know the differences between Unions and Structures in C
    programming.

    Thank you..
    Ravikiran, Nov 3, 2008
    #1
    1. Advertising

  2. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Ravikiran wrote:
    > I want know the differences between Unions and Structures in C
    > programming.


    The size of union is the size of its largest member. In most
    implementations (but standard does *not* require it) all members of
    union are placed at the same location in memory.
    Structures are just "normal" groups of variables.

    Pawel Dziepak

    PS I really shouldn't help you that way, there is a lot of information
    about structures and unions in many FAQs, articles, etc.
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.9 (GNU/Linux)
    Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org

    iEYEARECAAYFAkkO3r4ACgkQPFW+cUiIHNp5SwCgsATEiHUwMyAd4dGgVJQQAUR2
    MCoAn1+6G5wqv5h6JBxjP3x++K5SDBLT
    =z5G+
    -----END PGP SIGNATURE-----
    Pawel Dziepak, Nov 3, 2008
    #2
    1. Advertising

  3. On Mon, 03 Nov 2008 12:21:34 +0100,
    Pawel Dziepak <> wrote:
    >
    > Ravikiran wrote:
    >> I want know the differences between Unions and Structures in C
    >> programming.

    >
    > The size of union is the size of its largest member. In most
    > implementations (but standard does *not* require it) all members of
    > union are placed at the same location in memory.


    From 6.2.5 - 20

    — A union type describes an overlapping nonempty set of member
    objects, each of which has an optionally specified name and possibly
    distinct type.

    Also, from 6.5.8 - 5

    All pointers to members of the same union object compare equal.

    I'd say that pretty much gurantees that the members of a union, as you
    say it, are placed at the same location in memory.

    Martien

    PS. quotes from n1256. Wording in actual c99 standard is identical. I
    don't own a c89 copy, but in a draft for that standard, similar ior
    identical wording for the above quoted is present

    --
    |
    Martien Verbruggen | Since light travels faster than sound, is
    | that why some people appear bright until you
    | hear them speak?
    Martien Verbruggen, Nov 3, 2008
    #3
  4. Ravikiran

    gk Guest

    On Nov 3, 6:06 pm, Martien Verbruggen <> wrote:
    > On Mon, 03 Nov 2008 12:21:34 +0100,
    >         Pawel Dziepak <> wrote:
    >
    >
    >
    > > Ravikiran wrote:
    > >> I want know the differences between Unions and Structures in C
    > >> programming.

    >
    > > The size of union is the size of its largest member. In most
    > > implementations (but standard does *not* require it) all members of
    > > union are placed at the same location in memory.

    >
    > From 6.2.5 - 20
    >
    >   — A union type describes an overlapping nonempty set of member
    >     objects, each of which has an optionally specified name and possibly
    >     distinct type.
    >
    > Also, from 6.5.8 - 5
    >
    >   All pointers to members of the same union object compare equal.
    >
    > I'd say that pretty much gurantees that the members of a union, as you
    > say it, are placed at the same location in memory.
    >
    > Martien
    >
    > PS. quotes from n1256. Wording in actual c99 standard is identical. I
    > don't own a c89 copy, but in a draft for that standard, similar ior
    > identical wording for the above quoted is present
    >
    > --
    >                         |
    > Martien Verbruggen      | Since light travels faster than sound, is
    >                         | that why some people appear bright until you
    >                         | hear them speak?


    the difference basically arises in the memory allocation.....while
    structure allocates memory separately to each of its members, the same
    is not the case with unions..it allocates memory only to the largest
    available member...the rest of them are allocated within this block of
    memory only
    gk, Nov 3, 2008
    #4
  5. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Martien Verbruggen wrote:
    > I'd say that pretty much gurantees that the members of a union, as you
    > say it, are placed at the same location in memory.


    Sorry, it was my fault. I heard that the location can be different, but
    I didn't check that. Anyway, sorry for providing incorrect information.

    Pawel Dziepak
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.9 (GNU/Linux)
    Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org

    iEYEARECAAYFAkkPB1cACgkQPFW+cUiIHNpcSgCeMdASw61Y1oIZvce1+NR12WXA
    xcsAn0jYzPyCUAKQsZmOnjbQydXs0M2P
    =xpZ8
    -----END PGP SIGNATURE-----
    Pawel Dziepak, Nov 3, 2008
    #5
  6. Ravikiran

    James Kuyper Guest

    gk wrote:
    ....
    > the difference basically arises in the memory allocation.....while
    > structure allocates memory separately to each of its members, the same
    > is not the case with unions..it allocates memory only to the largest
    > available member...the rest of them are allocated within this block of
    > memory only


    The standard does not prohibit a union from having a size significantly
    bigger than the size of its largest member. In some cases, it's a
    practical necessity that the size be larger than that.

    On systems where alignment matters, in order for a union to ensure
    correct alignment for any given member, the union itself must have an
    alignment requirement that is a multiple of the alignment requirement of
    that members. Since this must apply to all members at the same time, it
    must be a common multiple of all of those requirements. There's obvious
    reasons to preference the least common multiple, but that's not required
    by the standard. On most systems, where all alignment requirements are
    powers of 2, the least common multiple of the alignment requirements of
    the members of the union is the same as the largest alignment
    requirement; but on a hypothetical system where long has an alignment
    requirement of 3, and double has an alignment requirement of 5, a union
    that contains both must have a alignment requirement of at least 15.

    The size of any object must be a multiple of its alignment requirement,
    otherwise a contiguous array of such objects could not all be correctly
    aligned.

    The net result of this is that the size of a union could be quite a bit
    larger than the size of it's largest member. Just consider a simple
    case, where double has an alignment requirement of 8. Then the following
    union:

    union dcarr{
    double d;
    char carr[9];
    };

    would have to have a size that is at least 9 bytes, and also a multiple
    of 8 bytes. The smallest size meeting both requirements is 16 bytes.
    James Kuyper, Nov 3, 2008
    #6
  7. Ravikiran <> writes:
    > I want know the differences between Unions and Structures in C
    > programming.


    If your textbook doesn't explain this clearly, you need a new
    textbook. I recommend K&R2 (Kernighan & Ritchie, The C Programming
    Language, 2nd Edition).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Nov 3, 2008
    #7
  8. Ravikiran

    CBFalconer Guest

    Ravikiran wrote:
    >
    > I want know the differences between Unions and Structures in C
    > programming.


    Try the C standard. For example:

    6.7.2.1 Structure and union specifiers

    ...

    Semantics

    [#4] As discussed in 6.2.5, a structure is a type consisting
    of a sequence of members, whose storage is allocated in an
    ordered sequence, and a union is a type consisting of a
    sequence of members whose storage overlap.

    and a good deal more.

    Some useful references about C:
    <http://www.ungerhu.com/jxh/clc.welcome.txt>
    <http://c-faq.com/> (C-faq)
    <http://benpfaff.org/writings/clc/off-topic.html>
    <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf> (C99)
    <http://cbfalconer.home.att.net/download/n869_txt.bz2> (pre-C99)
    <http://www.dinkumware.com/c99.aspx> (C-library}
    <http://gcc.gnu.org/onlinedocs/> (GNU docs)
    <http://clc-wiki.net/wiki/C_community:comp.lang.c:Introduction>

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.
    CBFalconer, Nov 3, 2008
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jakk
    Replies:
    4
    Views:
    12,132
  2. Neil Zanella
    Replies:
    9
    Views:
    416
    Jeffrey D. Smith
    Oct 16, 2003
  3. Alfonso Morra
    Replies:
    11
    Views:
    713
    Emmanuel Delahaye
    Sep 24, 2005
  4. Jason Curl

    Unions and structures implementation in C

    Jason Curl, Oct 12, 2005, in forum: C Programming
    Replies:
    4
    Views:
    799
    Default User
    Oct 12, 2005
  5. Ravikiran

    Difference between Unions and Structures...

    Ravikiran, Nov 3, 2008, in forum: C Programming
    Replies:
    2
    Views:
    313
    MJ_India
    Nov 3, 2008
Loading...

Share This Page