0

I just installed VSCODE, MSYS2 and GCC on my W11 PC. This is a by default install.

In order to play with .bmp file I have a small piece of code using a struct for the header. Start of the struct definition is :

struct  bmp_header_st{
    uint16_t bfType;
    uint32_t bfSize;
    uint16_t bfReserved1;
    uint16_t bfReserved2;
    uint32_t bfOffBits;
    ...

then the variable for the header is declared: struct bmp_header_st bitmap_header;

When bitmap_header variable is loaded via a file read (fread( &bitmap_header, 1, hsize, pfile);), I do not get the expected values in the struct fields; but if I dump the memory area of the bitmap_header variable I have the exact image of the .bmp file loaded.

It seems that fields of the struct are not aligned where I expect them to be. That's confirmed by displaying fields adress with VSCode :

  • bfType is in 0xc8e53ff790
  • bfSize is in 0xc8e53ff794
  • bfReserved1 is in 0xc8e53ff798

That means that uint32_t is allocated on 4 bytes, which is ok, but also uint16_t which is not and this is why when I load the bmp file in memory, data are not at the right place.

Any idea ?, this is certainly not much, but I have no clue !

Memory dump of the Struct after having being loaded from file Adresses of the fields of the Struct

AIM65
  • 1
  • 1
    Often, taking the time to write down a problem is the first step to resolve it… I found the answer elsewhere in this site : what I encounter is padding, it is inherent to the Struct and is platform dependent. As I want to run the code on Intel and also on Arm cortex, I’ll change the way to get the field data after the fread. I ‘ll forget the struct and use independent variables pointing the read buffer. – AIM65 Dec 23 '22 at 08:46

0 Answers0