This is the mail archive of the cygwin@sourceware.cygnus.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

RE: Alignment of Variables


Thomas.Irlet@x400.gr.admin.ch <Thomas.Irlet@x400.gr.admin.ch> wrote:

>I am using EGCS V1.0 with the MINGW32 Headers. Now I have a problem wi
>th the alignment of variables in the structure "BITMAPFILEHEADER". The
> structure is defined as
>
>typedef struct tagBITMAPFILEHEADER {
> WORD     bfType;
> DWORD  bfSize;
> WORD     bfReserved1;
> WORD     bfReserved2;
> DWORD  bfOffBits;
>} BITMAPFILEHEADER;
>
>My problem was now, that the compiler aligns all variables to their na
>tural boundaries, that is WORD to wordbouderies, DWORD to longbounderi
>es. To achieve this, a dummy WORD is inserted after bfType. To read in
> a BMP-file, this scrambles the whole header.
>What can I do to prevent the compiler to "optimize" this structures. A
>re there any "PRAGMA"'s, that could be used. A compilerswitch is not s
>o preferable, because in this case, very structure stays unoptimized.


In my version of Defines.h I have added

#define PACKED __attribute__((packed))

And then in Structures.h I have BITMAPFILEHEADER defined like this:

struct tagBITMAPFILEHEADER
{
  WORD    bfType;
  DWORD   bfSize;
  WORD    bfReserved1;
  WORD    bfReserved2;
  DWORD   bfOffBits;
} PACKED;

typedef struct tagBITMAPFILEHEADER BITMAPFILEHEADER;

Notice firstly the use of PACKED after the structure definition. This tells
gcc to make the structure packed (i.e. no padding for alignment). Secondly
notice that the typedef and struct declarations are separated. I found that

typedef struct
{
...
} PACKED BITMAPFILEHEADER;

Did not result in a packed structure... I don't know why (a bug in gcc?
Misuse of the attribute on my part?).

Anyway, the given strategy seems to work for those structures which need
packing in the Win32 API headers.

Colin.

-- Colin Peters - colin at fu.is.saga-u.ac.jp
-- Saga Univ. Dept. of Information Science
-- http://www.geocities.com/Tokyo/Towers/6162/index.html
-- http://www.fu.is.saga-u.ac.jp/~colin/index.html



-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]