Size of structs containing unions

Discussion in 'C Programming' started by luke, Sep 30, 2005.

  1. luke

    luke Guest

    hi,
    in Visula C++ 6.0 I have declared a struct like this:

    typedef struct _WRITE_INPUT {

    ULONG DeviceNumber;
    ULONG RegisterNumber;
    union {
    USHORT ShortData;
    UCHAR CharData;
    };
    } WRITE_INPUT;

    I can't understand why, sizeof(WRITE_INPUT) returns 12. It should
    return 10, shouldn't it?

    sizeof(ULONG) = 4
    sizeof(ULONG) = 4
    sizeof(USHORT) = 2 (longest union field)

    4 + 4 + 2 = 10

    thanks
    bye
    luke
     
    luke, Sep 30, 2005
    #1
    1. Advertising

  2. luke

    Anand Guest

    luke wrote:
    > hi,
    > in Visula C++ 6.0 I have declared a struct like this:
    >
    > typedef struct _WRITE_INPUT {
    >
    > ULONG DeviceNumber;
    > ULONG RegisterNumber;
    > union {
    > USHORT ShortData;
    > UCHAR CharData;
    > };
    > } WRITE_INPUT;
    >
    > I can't understand why, sizeof(WRITE_INPUT) returns 12. It should
    > return 10, shouldn't it?
    >
    > sizeof(ULONG) = 4
    > sizeof(ULONG) = 4
    > sizeof(USHORT) = 2 (longest union field)
    >
    > 4 + 4 + 2 = 10
    >
    > thanks
    > bye
    > luke
    >

    c.l.c FAQ #2.13 would answer your question
     
    Anand, Sep 30, 2005
    #2
    1. Advertising

  3. luke

    luke Guest

    thank you,
    I thought it was a union problem
    bye
     
    luke, Sep 30, 2005
    #3
  4. "luke" <> writes:
    > in Visula C++ 6.0 I have declared a struct like this:
    >
    > typedef struct _WRITE_INPUT {


    Don't use identifiers starting with an underscore; they're reserved to
    the implementation. (It's slightly more complex than that, but it's
    safest just to avoid them.)

    > ULONG DeviceNumber;
    > ULONG RegisterNumber;
    > union {
    > USHORT ShortData;
    > UCHAR CharData;
    > };
    > } WRITE_INPUT;
    >
    > I can't understand why, sizeof(WRITE_INPUT) returns 12. It should
    > return 10, shouldn't it?
    >
    > sizeof(ULONG) = 4
    > sizeof(ULONG) = 4
    > sizeof(USHORT) = 2 (longest union field)
    >
    > 4 + 4 + 2 = 10


    The compiler is free to add padding after any member of a structure.
    In this case, it's probably adding 2 bytes of padding at the end to
    make the size of the structure a multiple of 4, so the ULONG members
    will be aligned properly if you have an array of structures.

    Incidentally, the names ULONG, USHORT, and UCHAR aren't particularly
    helpful. I presume they're typedefs (or macros?) for unsigned long,
    unsigned short, and unsigned char, respectively. Why not just use the
    names directly?

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Sep 30, 2005
    #4
  5. luke wrote:
    > hi,
    > in Visula C++ 6.0 I have declared a struct like this:
    >
    > typedef struct _WRITE_INPUT {
    >
    > ULONG DeviceNumber;
    > ULONG RegisterNumber;
    > union {
    > USHORT ShortData;
    > UCHAR CharData;
    > };
    > } WRITE_INPUT;
    >
    > I can't understand why, sizeof(WRITE_INPUT) returns 12. It should
    > return 10, shouldn't it?


    Please check the FAQ before posting.
    You might find <http://www.eskimo.com/~scs/C-faq/q2.13.html> enlightening.

    BTW: Avoid nonstandard typenames in postings. If ULONG, USHORT, and
    UCHAR are in fact unsigned long, unsigned short, and unsigned char, why
    not use the real typenames?
     
    Martin Ambuhl, Sep 30, 2005
    #5
  6. In article <>,
    "luke" <> wrote:

    > hi,
    > in Visula C++ 6.0 I have declared a struct like this:
    >
    > typedef struct _WRITE_INPUT {
    >
    > ULONG DeviceNumber;
    > ULONG RegisterNumber;
    > union {
    > USHORT ShortData;
    > UCHAR CharData;
    > };
    > } WRITE_INPUT;


    What is ULONG? What is USHORT? What is UCHAR?

    > I can't understand why, sizeof(WRITE_INPUT) returns 12. It should
    > return 10, shouldn't it?
    >
    > sizeof(ULONG) = 4
    > sizeof(ULONG) = 4
    > sizeof(USHORT) = 2 (longest union field)
    >
    > 4 + 4 + 2 = 10


    Compilers usually add padding (unused bytes) between members of a struct
    or at the end of a struct to align the data in the struct and to allow
    the processor to access them faster.

    If one member of a struct has a size of four bytes, then quite often the
    size of the struct will be increased to a multiple of four bytes, if
    necessary.
     
    Christian Bau, Sep 30, 2005
    #6
  7. luke

    Jack Klein Guest

    On 30 Sep 2005 00:23:18 -0700, "luke" <> wrote in
    comp.lang.c:

    > hi,
    > in Visula C++ 6.0 I have declared a struct like this:


    Aside from the other replies you received, your code is not C at all,
    and does not belong in this newsgroup in any way, shape, or form.

    >
    > typedef struct _WRITE_INPUT {
    >
    > ULONG DeviceNumber;
    > ULONG RegisterNumber;
    > union {
    > USHORT ShortData;
    > UCHAR CharData;
    > };


    There is no such thing as an anonymous union in C. A conforming C
    compiler must issue a diagnostic for this.

    Although it appears that Visual C++ 6.0 is non-conforming in this
    respect.

    > } WRITE_INPUT;


    [snip]

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Oct 1, 2005
    #7
  8. luke

    Ravi Uday Guest

    Keith Thompson wrote:

    > "luke" <> writes:
    >
    >>in Visula C++ 6.0 I have declared a struct like this:
    >>
    >>typedef struct _WRITE_INPUT {

    >
    >
    > Don't use identifiers starting with an underscore; they're reserved to
    > the implementation. (It's slightly more complex than that, but it's
    > safest just to avoid them.)
    >
    >

    sorry to be nitty, but since some of us here do work at the OS/kernel
    level and use C, I think using underscore when declaring objects are
    fine (it would be a part of bug fix ) ?

    - Ravi


    >> ULONG DeviceNumber;
    >> ULONG RegisterNumber;
    >> union {
    >> USHORT ShortData;
    >> UCHAR CharData;
    >> };
    >>} WRITE_INPUT;
    >>
    >>I can't understand why, sizeof(WRITE_INPUT) returns 12. It should
    >>return 10, shouldn't it?
    >>
    >>sizeof(ULONG) = 4
    >>sizeof(ULONG) = 4
    >>sizeof(USHORT) = 2 (longest union field)
    >>
    >>4 + 4 + 2 = 10

    >
    >
    > The compiler is free to add padding after any member of a structure.
    > In this case, it's probably adding 2 bytes of padding at the end to
    > make the size of the structure a multiple of 4, so the ULONG members
    > will be aligned properly if you have an array of structures.
    >
    > Incidentally, the names ULONG, USHORT, and UCHAR aren't particularly
    > helpful. I presume they're typedefs (or macros?) for unsigned long,
    > unsigned short, and unsigned char, respectively. Why not just use the
    > names directly?
    >
     
    Ravi Uday, Oct 14, 2005
    #8
  9. Ravi Uday a écrit :
    >
    > Keith Thompson wrote:
    >
    >> "luke" <> writes:
    >>
    >>> in Visula C++ 6.0 I have declared a struct like this:
    >>>
    >>> typedef struct _WRITE_INPUT {

    >>
    >> Don't use identifiers starting with an underscore; they're reserved to
    >> the implementation. (It's slightly more complex than that, but it's
    >> safest just to avoid them.)
    >>

    > sorry to be nitty, but since some of us here do work at the OS/kernel
    > level and use C, I think using underscore when declaring objects are
    > fine (it would be a part of bug fix ) ?


    Implementation means compiler and compiler's libraries. Linux Kernel
    Modules or the like are part of the system, not of the implmentation.
     
    Emmanuel Delahaye, Oct 14, 2005
    #9
  10. luke

    pete Guest

    Emmanuel Delahaye wrote:

    > Implementation means compiler and compiler's libraries. Linux Kernel
    > Modules or the like are part of the system, not of the implmentation.


    I think "implementation" means everything, including hardware.
    If you change the system, then you change the implementation.

    N869
    3.10
    [#1] implementation
    a particular set of software, running in a particular
    translation environment under particular control options,
    that performs translation of programs for, and supports
    execution of functions in, a particular execution
    environment

    --
    pete
     
    pete, Oct 14, 2005
    #10
  11. luke

    Alan Balmer Guest

    On Fri, 14 Oct 2005 17:10:07 +0530, Ravi Uday <>
    wrote:

    >
    >Keith Thompson wrote:
    >
    >> "luke" <> writes:
    >>
    >>>in Visula C++ 6.0 I have declared a struct like this:
    >>>
    >>>typedef struct _WRITE_INPUT {

    >>
    >>
    >> Don't use identifiers starting with an underscore; they're reserved to
    >> the implementation. (It's slightly more complex than that, but it's
    >> safest just to avoid them.)
    >>
    >>

    >sorry to be nitty, but since some of us here do work at the OS/kernel
    >level and use C, I think using underscore when declaring objects are
    >fine (it would be a part of bug fix ) ?


    Well, no. OS/kernel is not part of the C implementation. However, if
    the system APIs you're working with use identifiers beginning with
    underscore, you're stuck with it, for all practical purposes. I just
    hope other aspects of the API don't show the same ignorance of
    standards.

    BTW, do you think the OP is really doing OS/kernel work using Visual
    C++ 6.0?
    >
    >- Ravi
    >
    >
    >>> ULONG DeviceNumber;
    >>> ULONG RegisterNumber;
    >>> union {
    >>> USHORT ShortData;
    >>> UCHAR CharData;
    >>> };
    >>>} WRITE_INPUT;
    >>>
    >>>I can't understand why, sizeof(WRITE_INPUT) returns 12. It should
    >>>return 10, shouldn't it?
    >>>
    >>>sizeof(ULONG) = 4
    >>>sizeof(ULONG) = 4
    >>>sizeof(USHORT) = 2 (longest union field)
    >>>
    >>>4 + 4 + 2 = 10

    >>
    >>
    >> The compiler is free to add padding after any member of a structure.
    >> In this case, it's probably adding 2 bytes of padding at the end to
    >> make the size of the structure a multiple of 4, so the ULONG members
    >> will be aligned properly if you have an array of structures.
    >>
    >> Incidentally, the names ULONG, USHORT, and UCHAR aren't particularly
    >> helpful. I presume they're typedefs (or macros?) for unsigned long,
    >> unsigned short, and unsigned char, respectively. Why not just use the
    >> names directly?
    >>

    --
    Al Balmer
    Balmer Consulting
     
    Alan Balmer, Oct 14, 2005
    #11
  12. luke

    Alan Balmer Guest

    On Fri, 14 Oct 2005 15:01:00 GMT, pete <> wrote:

    >Emmanuel Delahaye wrote:
    >
    >> Implementation means compiler and compiler's libraries. Linux Kernel
    >> Modules or the like are part of the system, not of the implmentation.

    >
    >I think "implementation" means everything, including hardware.
    >If you change the system, then you change the implementation.
    >
    >N869
    > 3.10
    > [#1] implementation
    > a particular set of software, running in a particular
    > translation environment under particular control options,
    > that performs translation of programs for, and supports
    > execution of functions in, a particular execution
    > environment


    Re-read the reference. "A particular set of software" is the subject
    of the sentence. The rest is description of that set of software.

    In the sense of the standard, the hardware is not part of the
    implementation, though the implementation can support a particular set
    of hardware.
    --
    Al Balmer
    Balmer Consulting
     
    Alan Balmer, Oct 14, 2005
    #12
  13. luke

    Guest

    There is concept of structure padding. it depends on compiler to
    compiler , when u use the turbo C compiler u will find the size as 10
    but VC++ always use structure padding that is why it is 12.
    Some compilers usually use the space in the multiple of even nos like
    2,4,or 8..
    if the size of structure is not multiple of predecided then it add the
    required no of bytes.
     
    , Oct 15, 2005
    #13
  14. luke

    pete Guest

    Alan Balmer wrote:
    >
    > On Fri, 14 Oct 2005 15:01:00 GMT, pete <> wrote:
    >
    > >Emmanuel Delahaye wrote:
    > >
    > >> Implementation means compiler and compiler's libraries. Linux Kernel
    > >> Modules or the like are part of the system, not of the implmentation.

    > >
    > >I think "implementation" means everything, including hardware.
    > >If you change the system, then you change the implementation.
    > >
    > >N869
    > > 3.10
    > > [#1] implementation
    > > a particular set of software, running in a particular
    > > translation environment under particular control options,
    > > that performs translation of programs for, and supports
    > > execution of functions in, a particular execution
    > > environment

    >
    > Re-read the reference. "A particular set of software" is the subject
    > of the sentence. The rest is description of that set of software.
    >
    > In the sense of the standard, the hardware is not part of the
    > implementation, though the implementation can support a particular set
    > of hardware.


    Thank you.
    What is an "environment"
    as in "translation environment" and "execution environment"?

    --
    pete
     
    pete, Oct 15, 2005
    #14
    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. Udo Steinberg
    Replies:
    3
    Views:
    592
    Udo Steinberg
    May 15, 2004
  2. Neil Zanella
    Replies:
    1
    Views:
    457
    Jack Klein
    Sep 17, 2003
  3. tyler
    Replies:
    0
    Views:
    310
    tyler
    Sep 4, 2006
  4. Lokicer

    The size of structs containing "int a[0]"?

    Lokicer, Oct 16, 2005, in forum: C Programming
    Replies:
    4
    Views:
    351
    Harry Plaske
    Oct 16, 2005
  5. unions & structs...

    , Jan 17, 2006, in forum: C Programming
    Replies:
    18
    Views:
    590
    Ingo Menger
    Jan 18, 2006
Loading...

Share This Page