Monday, May 24, 2010

Bitmap header file loading error using C language?

I'm working with bitmap image using c programming. This my header file structure shown below:





word bmpType;


dword bmpSize;


word bmpReserved1;


word bmpReserved2;


dword bOffBits;





The header file should be 14 byte as shown above, but when i use sizeof() for the structure above, it returned 16. This problem causes the data loaded inside the member of the structure to be mistake since everythings are shifted with two bytes. I've looked through my code, and it seems no problem for the code. Please let me know what is the cause of this problem and how to solve it.

Bitmap header file loading error using C language?
Your problem has to do with the structure member alignment when the compiler generates the code. In MS VS the default is on a 4 byte boundary e.g. 4, 8 12, 16, 20, etc.





You can change this by going into the property page for the project (project|property) and selecting c/c++/Code Generation option, and change Struct Member Alignment to 1 byte.


This info is for MS VS 2005.





if you are using some other dev environment, there should be something similar.

long stem roses

No comments:

Post a Comment