GP32 GCC structure alignment


Feeblez

Member
Joined
Mar 10, 2003
Messages
172
does anyone know how i can byte align structures in a header file so they don't get padded with extra space?

normally i would do:
e.g.

#pragma pack(push)
#pragma pack(1)

struct sBmpFileHeader
{
word bfType;
dword bfSize;
word bfReserved1;
word bfReserved2;
dword bfOffBits;
};

#pragma pack(pop)

but GCC compiler outputs "ignoring pragmas" ! (or something like) :(
 
Hi.

To pack structs, you can use the -fpack-struct option.

Always try not to use pragmas, the behaviour is so unknown from one version to another...

Anyway packed structs should not be used, since it decrease performances in accessing data. The only (and bad) reason to use packed structs should be to cast a raw data into a structure (i.e. a BMP header into something usable)... This is way faster than copying each byte of the raw data into the right cell of your struct, but it will be slower to acces the data contained in the struct...

But you code whatever you want, the way you want :)

Wammy
 
pragmas are used more for Microsoft stuff, as wammy said, avoiding them is best. You want porting, so any gcc options you use will work for most platforms, pragmas will not.

- Rico
 
thanks for the reply! :)

i guess the main reason i wanted to pack the structure is so that i can use sizeof(sBmpFileHeader) and read the correct number of bytes from an external file, and also use sizeof to calculate how far to seek into the file. (BMPs and TGAs need byte alignment)

the packed structures will be discarded after i've transferred the relevant information into my own internal structures, so it's not really a performance issue.

as you say -fpack-struct isn't such a good idea, especially as i cant limit it to just a couple of structures.

i'll just access the file data in small steps with a char* to get around any alignment issues. <_<
 
To pack structures individually in gcc, use

struct blah {
...
} __attribute__ ((packed));
 
thanks, i read about that earlier today but wasn't sure it was gcc

says it only works with plain c tho, and not c++

guess what my bmp loader is? :D

oh well i dont mind making it a c module,
it is worth it in this instance :)
 
Back
Top