0

I have found such strange thing: for the MasterBootRecord structure

//

typedef unsigned char Byte;
typedef unsigned short int Word;
typedef unsigned int Dword;

//
typedef struct
{
    Byte currentPartitionState;

    Byte startPartitionHead;
    Word startCylinderSector;

    Byte partitionType;

    Byte endPartitionHead;
    Word endCylinderSector;

    Dword numSectorsBetweenMbrAndPartition;
    Dword numSectorsInPartition;
} PartitionEntry;

//
typedef struct
{
    Byte executableCode[446];

    PartitionEntry partitionEntry1;
    PartitionEntry partitionEntry2;
    PartitionEntry partitionEntry3;
    PartitionEntry partitionEntry4;

    Word bootRecordSignature;
} MasterBootRecord;

when I try to get sizeof(MasterBootRecord), I get 516 bytes, although right size is 446 + 4*16 + 2 = 512. Why it could be so???

deathmood
  • 113
  • 6

1 Answers1

1

It's because of data alignment in memory.

The thing is that to store your 446 bytes in 4byte chunks, you need 112 such chunks, which occupy 448 bytes (2 bytes more than expected). And for storing the last Word you need also 4 bytes instead of 2 (2 bytes more). So there are your 4 extra bytes.

You can try commenting the "bootRecordSignature" and you should still get 2 more bytes than expected (the expected is 510 but you will get 512).

Daniel
  • 21,933
  • 14
  • 72
  • 101