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

J

JR

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;
 
C

Christoph Rabel

JR said:
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
 
G

Gianni Mariani

JR said:
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;
 
X

Xenos

JR said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top