Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Why different sizes (with unions) when __int64 used???

Reply
Thread Tools

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

 
 
JR
Guest
Posts: n/a
 
      02-03-2004
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;
 
Reply With Quote
 
 
 
 
Christoph Rabel
Guest
Posts: n/a
 
      02-03-2004
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
 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      02-03-2004
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;


 
Reply With Quote
 
Xenos
Guest
Posts: n/a
 
      02-05-2004

"JR" <> wrote in message
news: om...
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Win 7 changing font sizes without icon sizes? why? Computer Support 0 03-21-2010 11:32 AM
Re: Win 7 changing font sizes without icon sizes? why? Computer Support 0 03-21-2010 11:31 AM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
The File Sizes of Pictures on my CDs Increased to Unreadable Sizes Marful Computer Support 11 03-08-2006 07:13 PM
Re: Different results if compared as double or __int64? John Tsiombikas (Nuclear / the Lab) C++ 1 06-30-2003 02:31 PM



Advertisments