How to make my compiler stop padding structs

Started by
5 comments, last by Zahlman 18 years, 9 months ago
I just wrote a BMP loader from scratch that didn't use any structs. The first 14 bytes of a bmp file are:

byte# - variable name
2  	bfType
4  	bfSize
2  	bfReserved1
2  	bfReserved2
4  	bfOffBits
I had to use auto variables because whenever I would create a struct (Called BitmapFileHeader) that SHOULD be 14 bytes, mingw would pad it to 16 bytes. When I loaded it from the file stream, I would get improper values, and saving it was wrong too (of course). What I want to know is how I can get MinGw to stop padding my structs out. It's no big deal because the loader I just wrote works perfectly, but it's a lot more convenient to be able to use structs. Thanks for any help.
Advertisement
In mingw (and also GCC) you can do like this:
#pragma pack(push, 1)struct BitmapFileHeader{  /* struct members go here */};#pragma pack(pop)


But be aware that this can have impact on speed as 32bits processors are accessing memory at 4bytes boundaries. I would suggest loading every member individually.

Also note that this will only work on mingw and GCC, Microsoft Visual C++ uses another directive to pack structs.

If i remember correctly, there should be a struct in windows.h for bitmap files headers but don't remember the name, a quick google should give you the answer.

Hope that helps !
Matt
Matt
well yeah there IS a bitmap struct, but I don't want to use it 'cause I like doing stuff like this :) Anyway, thanks for the #pragma command. Rating++ (oops, already rated you up I think)
Oh, and I'll probably end up loading the stuff individually, but when I'm still hacking around I like the convenient solution.
Thanks man!
14%4 is non zero. Is saving two measly bytes worth reading unaligned data?

Never mind, this is for loading from a file. Well, endianness and word sizes will still bork it up. Hooray.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
I'm already compensating for Big and Little endianness in my PutPixel function and word sizes haven't been a problem, so I think I'm okay.
Quote:Original post by lemurion
Also note that this will only work on mingw and GCC, Microsoft Visual C++ uses another directive to pack structs.

According to this, Microsoft also supports it as extended syntax.

Another method of doing this in GCC is __attribute__((packed)), which is what I've been using so far. However, lemurion's approach appears to be compatible with both GCC and MSVC, so I'll be switching over.
Note that you don't have to use "auto variables" (I assume that you specifically mean separate auto vars for each value) in order to read the values individually; you can keep them wrapped in a struct, and just read in to structInstance.fieldName . :)

This topic is closed to new replies.

Advertisement