From: colin AT fu DOT is DOT saga-u DOT ac DOT jp (Colin Peters) Subject: RE: Alignment of Variables 16 Apr 1998 10:37:09 -0700 Message-ID: <003d01bd68e8$6af6c320$fa173185.cygnus.gnu-win32@gbird0.fu.is.saga-u.ac.jp> Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit To: Cc: "GNU-Win32" Thomas DOT Irlet AT x400 DOT gr DOT admin DOT 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 AT cygnus DOT com" with one line of text: "help".