Re: sizeof (struct) differs from Windows and Linux. How can I fix this?
- From: Bob Caryl <bob fis-cal com>
- To: John Cupitt <jcupitt gmail com>
- Cc: gtk-app-devel-list gnome org, edward hage <edha xs4all nl>
- Subject: Re: sizeof (struct) differs from Windows and Linux. How can I fix this?
- Date: Thu, 30 Dec 2004 09:49:22 -0600
On Wed, 2004-12-29 at 12:06, John Cupitt wrote:
On Wed, 29 Dec 2004 18:57:41 +0100, edward hage <edha xs4all nl> wrote:
Now on Linux the size of the structs are resp. : 388 and 172.
And on Windows XP (cross-compile mingw on Linux): 392 and 176.
This is part of C: you can't write() a struct on one machine and
read() it on another. They may have different alignment rules (I think
this is the cause of your problem), or even different endian-ness.
In case specified by Edward dealing with programs written to run on
Linux and Windows XP, John is mistaken when he says, ". . . you can't
write() a struct on one machine and read() it on another."
I work for a company that produces a business management system for
truck stops. We run our back office accounting software on Linux and
our point-of-sale runs on Windows XP based machines. We use the same
data files on both, they contain binary data, and are interchangeable.
The key to being able to write() and read() structures between the two
operating systems is to set your compiler structure member alignment to
one byte on both platforms.
The Microsoft development system defaults to padding structures out to
even numbers of bytes that are divisible by 8 (the size of a double
precision floating point data type). You can set the compiler to one
byte structure member alignment in the project settings dialog on the
C/C++ tab and select Code Generation on the Category drop down box.
Structure member alignment is one of the parameters that appears. Set
this to 1 byte and, on your g++ compile line, include the
'-fpack-struct' directive to compel the Gnu compiler to do the same
There is, however, one caveat: you must be sure to arrange your data
structure such that the largest data types are listed first, followed by
the next largest and so on until you reach char types, to wit:
double dfoo1; // largest
float ffoo1; // next largest
int ifoo1; // and so on . . .
char cfoo1; // inserting character data last
This will insure that you will not have alignment problems on either
John also mentioned endian type problems. You will have that sort of
problem regardless of how you store your data on disk in files destined
to be read on other machines and is, therefore, not a structure padding
I hope this helps.
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
] [Thread Prev