Re: sizeof (struct) differs from Windows and Linux. How can I fix this?



On Wed, 2004-12-29 at 12:06, John Cupitt wrote:
Hi Edward,

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.

Hello All:

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
thing.  

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:

struct rfoo
{
        double  dfoo1;          // largest
        double  dfoo2;
        float   ffoo1;          // next largest
        float   ffoo2;
        int     ifoo1;          // and so on . . .
        char    cfoo1[27];      // inserting character data last
        char    cfoo2[15];
};

This will insure that you will not have alignment problems on either
target machine.

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
issue.

I hope this helps.

Best regards,
Bob Caryl 

_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]