Why different sizes (with unions) when __int64 used???

Discussion in 'C++' started by JR, Feb 3, 2004.

  1. JR

    JR Guest

    Take a look at TestStruct1 and TestStruct2. Clearly, the D2 part of
    the union is MUCH larger than the D1 portion. I would expect
    sizeof(TestStruct1)==sizeof(TestStruct2) but that is not the case
    using Microsoft Visual C++ 2003. Here is what I get

    sizeof(TestStruct1)==0x108
    sizeof(TestStruct2)==0x104

    Is this normal C++ compiler behavior, or a bug in the compiler?

    typedef struct _TestStruct1 {
    union {
    struct {
    unsigned __int64 n1;
    } D1;
    struct {
    int n3[64];
    int n4;
    } D2;
    } u;
    } TestStruct1, *PTestStruct1;

    typedef struct _TestStruct2 {
    union {
    struct {
    int n1;
    } D1;
    struct {
    int n3[64];
    int n4;
    } D2;
    } u;
    } TestStruct2, *PTestStruct2;
    JR, Feb 3, 2004
    #1
    1. Advertising

  2. JR wrote:
    > Take a look at TestStruct1 and TestStruct2. Clearly, the D2 part of
    > the union is MUCH larger than the D1 portion. I would expect
    > sizeof(TestStruct1)==sizeof(TestStruct2) but that is not the case
    > using Microsoft Visual C++ 2003. Here is what I get
    >
    > sizeof(TestStruct1)==0x108
    > sizeof(TestStruct2)==0x104
    >
    > Is this normal C++ compiler behavior, or a bug in the compiler?


    The compiler is allowed to add padding bytes to the struct
    to give types a valid alignment. So the behaviour of sizeof
    is correct, albeit implementation defined.

    Be careful, other compilers may give you different values,
    even the expected equality, may it be 108 or 104.

    > typedef struct _TestStruct1 {
    > union {
    > struct {
    > unsigned __int64 n1;


    This is an implementation defined type, the standard doesnt
    define __int64.

    hth

    Christoph
    Christoph Rabel, Feb 3, 2004
    #2
    1. Advertising

  3. JR wrote:
    > Take a look at TestStruct1 and TestStruct2. Clearly, the D2 part of
    > the union is MUCH larger than the D1 portion. I would expect
    > sizeof(TestStruct1)==sizeof(TestStruct2) but that is not the case
    > using Microsoft Visual C++ 2003. Here is what I get
    >
    > sizeof(TestStruct1)==0x108
    > sizeof(TestStruct2)==0x104


    This is implementation dependant - however, think about alignment of an
    array of these - For the second element in the array to have the correct
    alignment the whole object has to have a size that is a multiple of the
    largest alignment of all members in the struct.

    104 is not divisible by alignof( __int64 ) - 108 is the next multiple of
    alignof( __int64 ). (alignof being a figment of my imagination).

    Of course __int64 is a figment of Microsoft's imagination. (long long
    is also not strictly part of the standard but at least that is a
    standard C99 type and will probably be adopted as part of the C++
    standard some time in the future and most other compilers support it
    already).

    >
    > Is this normal C++ compiler behavior, or a bug in the compiler?
    >
    > typedef struct _TestStruct1 {
    > union {
    > struct {
    > unsigned __int64 n1;
    > } D1;
    > struct {
    > int n3[64];
    > int n4;
    > } D2;
    > } u;
    > } TestStruct1, *PTestStruct1;
    >
    > typedef struct _TestStruct2 {
    > union {
    > struct {
    > int n1;
    > } D1;
    > struct {
    > int n3[64];
    > int n4;
    > } D2;
    > } u;
    > } TestStruct2, *PTestStruct2;
    Gianni Mariani, Feb 3, 2004
    #3
  4. JR

    Xenos Guest

    "JR" <> wrote in message
    news:...
    > Take a look at TestStruct1 and TestStruct2. Clearly, the D2 part of
    > the union is MUCH larger than the D1 portion. I would expect
    > sizeof(TestStruct1)==sizeof(TestStruct2) but that is not the case
    > using Microsoft Visual C++ 2003. Here is what I get
    >
    > sizeof(TestStruct1)==0x108
    > sizeof(TestStruct2)==0x104
    >
    > Is this normal C++ compiler behavior, or a bug in the compiler?
    >
    > typedef struct _TestStruct1 {
    > union {
    > struct {
    > unsigned __int64 n1;
    > } D1;
    > struct {
    > int n3[64];
    > int n4;
    > } D2;
    > } u;
    > } TestStruct1, *PTestStruct1;
    >
    > typedef struct _TestStruct2 {
    > union {
    > struct {
    > int n1;
    > } D1;
    > struct {
    > int n3[64];
    > int n4;
    > } D2;
    > } u;
    > } TestStruct2, *PTestStruct2;


    VC++ does "funky" things with structs and the like when mixing types. Try
    using pragma pack.

    DrX
    Xenos, Feb 5, 2004
    #4
    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. John Tsiombikas (Nuclear / the Lab)

    Re: Different results if compared as double or __int64?

    John Tsiombikas (Nuclear / the Lab), Jun 30, 2003, in forum: C++
    Replies:
    1
    Views:
    418
    Victor Bazarov
    Jun 30, 2003
  2. Vivi
    Replies:
    2
    Views:
    5,976
    ES Kim
    Aug 25, 2003
  3. Tim Clacy

    __int64 vs. long long

    Tim Clacy, Nov 10, 2003, in forum: C++
    Replies:
    8
    Views:
    809
    Rolf Magnus
    Nov 10, 2003
  4. rohit
    Replies:
    23
    Views:
    1,721
    Dave Thompson
    Jun 10, 2004
  5. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,762
    Smokey Grindel
    Dec 2, 2006
Loading...

Share This Page