[prev in list] [next in list] [prev in thread] [next in thread] 

List:       koffice-devel
Subject:    Re: Portable Code (am I dreaming? :))
From:       Nicolas Goutte <nicog () snafu ! de>
Date:       2002-01-27 21:13:50
[Download RAW message or body]

On Sunday 27 January 2002 11:28, Clarence Dang wrote:
> On Sat, 26 Jan 2002 10:13, shaheed wrote:
> > > Can all the packing/alignment business be avoided if I get rid of
> > > sizeof()s and instead have #define <structure name>_SIZEOF everywhere
> > > instead?
> >
> > Yes. That's exactly my plan. Nicolas, I think that Clarence is talking
> > about the packing/alignment crap in the ms word filter. I did that stuff
> > precisely so that I could use sizeof() in certain places. It would have
> > worked just fine too, if only certain stupid compilers (e.g. on Irix) had
> > controls over packing and alignment!
>
> I just tried implementing this i.e. no more #pragma pack (1).
> I thought that all the compiler would do is add some padding to the end of
> each structure/class and align the start of each structure. But instead of
> just doing that, it's also trying to align some of my structure elements on
> a boundary of 4 e.g.:

A structure might be padded after each member variable. Note that an array 
just counts as one variable, so you cannot have padding inside the array.

>
> // Header for all MSWrite documents
> class MSWRITE_HEADER
> {
> public:
> 	WORD magic;
> 	WORD zero;
> 	WORD magic2;
> 	WORD reserved [4];

Here is the extra padding!

> 	DWORD numBytes;
>
> The offset of numBytes from the start of the structure is 14 (with #pragma
> pack(1)) but without it, it is 16 (and this is happening on i386!!! - not
> some new 64-bit system). This is a major problem!  What should I do?

The reason is that you have 7 times WORD, which makes 14 bytes. As DWORD is 
32 bits wide, it must be aligned at a 32 bit boundary. The next one available 
is at 16.

These values are true for the gcc that you are using. Another compiler might 
have a different behaviour. For example, take a compiler for a 32 bit 
processor that needs 16 bit values aligned at 32 bit boundary. This compiler 
would probably pad after each WORD. numBytes would then be at 24.

>
> Do I now have to read one element at a time, like everyone has been telling
> me to do so? Don't tell me I have to say goodbye to fread (buffer, sizeof
> (MSWRITE_HEADER), 1, filepointer)!

Well, if you want a portable version (or for the portable part of your code), 
you cannot use fread (buffer, sizeof(MSWRITE_HEADER), 1, filepointer) As I 
already said before, for portable code, you have to read byte per byte. 

You may have a choice of the exact implementation. For example QT's 
QDataStream does it a little different than my examples. But somewhere in the 
code, you have to be byte per byte to be portable.

As for your unportable code (little endian 32 bit CPU), you can continue to 
use pack(1).

And if someone is using a compiler that cannot control packing, the compiler 
can be added to the cases where you need the portable code (even if the CPU 
is 32 bits and little endian.)

>
> Thanks!
> Clarence

Have a nice day/evening/night!
_______________________________________________
koffice-devel mailing list
koffice-devel@mail.kde.org
http://mail.kde.org/mailman/listinfo/koffice-devel
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic