Connecting Tech Pros Worldwide Forums | Help | Site Map

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

JR
Guest
 
Posts: n/a
#1: Jul 22 '05
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;

Christoph Rabel
Guest
 
Posts: n/a
#2: Jul 22 '05

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


JR wrote:[color=blue]
> 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?[/color]

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.
[color=blue]
> typedef struct _TestStruct1 {
> union {
> struct {
> unsigned __int64 n1;[/color]

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

hth

Christoph
Gianni Mariani
Guest
 
Posts: n/a
#3: Jul 22 '05

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


JR wrote:[color=blue]
> 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[/color]

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).
[color=blue]
>
> 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;[/color]

Xenos
Guest
 
Posts: n/a
#4: Jul 22 '05

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



"JR" <russet32@hotmail.com> wrote in message
news:7d049c08.0402021732.60a56ad3@posting.google.c om...[color=blue]
> 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;[/color]

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

DrX


Closed Thread