Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sizeof ( struct ) != sum( sizeof ( struct.member )) ???

Status
Not open for further replies.

Autechre

MIS
Jul 30, 2002
3
US
Hello - I'm having this problem in VC++.NET:
For the following struct:
typedef struct _Tran
{
short trndelflag;
short trnrectype;
unsigned long trnpatnum;
unsigned long trnseqnum;
unsigned long trnadmit;
unsigned long trndscharge;
unsigned long trnloc1;
unsigned long trnloc2;
char trncod1[4];
char trncod2[4];
char trncod3[4];
char trnsecurity;
char trnstorage;
} TRAN;
If you add up the individual members' sizes, you get 42: either by the book or by computing sizeof(TRAN.trndelflag) + sizeof( TRAN.trnrectype ) + ...etc.

However, sizeof( TRAN ) returns 44!? At first I thought this had something to do with the alignment or whatever, but with any other structure I tried, sum( sizeof( members )) is ALWAYS = sizeof( struct ).

Even in this struct, if you change trncod1[4] to trncod1[2], you get both sizes = 40.

What's going on? Thanks...
 
it IS a padding issue. Here is what is happening.

You are padding to a 32 bit boundary

sizeof(long) = 4
sizeof(short) = 2
sizeof(char) = 1

S = short
L = long
C = char
P = Padding

your struct in memory looks like

SSSS
LLLL
LLLL
LLLL
LLLL
LLLL
LLLL
LLLL
LLLL
LLLL
CCCC
CCCC
CCCC
CCPP

This is where your issue lies. It just so happened that your previous attempts were padded correctly or you were padding to a different word boundary. When doing pointer arithmetic ALWAYS use sizeof(struct) if using a BYTE pointer. If you are using a struct pointer, a ++ is fine.

Matt

 
I sort of felt this is what it was all about. In fact this is old 16 bit code that used these structs to write stuff to disk. Now my new 32 bit code attempts to read the old data, and every time I did a fread( &struct, sizeof(struct ), .. ) the struct that was read was garbage when the individual members were printf'd; the FIRST record however is ok, things start looking crappy starting with the second.

The thing is the struct had been written out with a fwrite by the original DOS code. But I guess because of the 16 bit alignment, there was no padding in that case. So basically now I am reading 2 chars too many every time...

Anyway, thanks bunches. I had fixed the problem by fread-ing( &struct, sizeof( struct ) -2 ... ) but had no explanation for it.
 
You can still do that, it will just require one of 2 things. You physically add the padding by adding a char for each byte you need to pad to 4. The second is configuring vc++ to padd to a 2 BYTE boundary. I am not sure how to do this, but someone in the past has done it. You should find it on a search of the microsoft C++ forums.

Matt
 
I think you can do that by putting before the struct

#pragma pack(push,2)

and after it

#pragma pack(pop)

Try that.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top