Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is sizeof(IO_ERROR_LOG_PACKET) == 48 and not 44?

typedef struct _IO_ERROR_LOG_PACKET {
  UCHAR MajorFunctionCode; offset: 0 byte, size: 1 byte
  UCHAR RetryCount       ; offset: 1 byte, size: 1 byte
  USHORT DumpDataSize    ; offset: 2 byte, size: 2 byte
  USHORT NumberOfStrings ; offset: 4 byte, size: 2 byte
  USHORT StringOffset    ; offset: 6 byte, size: 2 byte
  USHORT EventCategory   ; offset: 8 byte, size: 2 byte + 2 byte for alignment
  NTSTATUS ErrorCode     ; offset: 12 byte, size: 4 byte
  ULONG UniqueErrorValue ; offset: 16 byte, size: 4 byte
  NTSTATUS FinalStatus   ; offset: 20 byte, size: 4 byte
  ULONG SequenceNumber   ; offset: 24 byte, size: 4 byte
  ULONG IoControlCode    ; offset: 28 byte, size: 4 byte
  LARGE_INTEGER DeviceOffset; offset: 32 byte, size: 8 byte
  ULONG DumpData[1]      ; offset: 40 byte, size: 4 byte
} IO_ERROR_LOG_PACKET, *PIO_ERROR_LOG_PACKET;

; total = 44 byte

I expected sizeof(IO_ERROR_LOG_PACKET) to be 44 bytes. But when I disassembled it, it turned out to be 48 bytes. Does someone know why?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

LARGE_INTEGER is a 64-bit (8-byte) integer. It causes the entire struct to be 8-byte aligned:

typedef struct _IO_ERROR_LOG_PACKET {
  UCHAR MajorFunctionCode; 1 byte
  UCHAR RetryCount       ; 1 byte
  USHORT DumpDataSize    ; 2 byte
  USHORT NumberOfStrings ; 2 byte
  USHORT StringOffset    ; 2 byte
  USHORT EventCategory   ; 2 byte + 2 byte for alignment
  NTSTATUS ErrorCode     ; 4 byte
  ULONG UniqueErrorValue ; 4 byte
  NTSTATUS FinalStatus   ; 4 byte
  ULONG SequenceNumber   ; 4 byte
  ULONG IoControlCode    ; 4 byte
  LARGE_INTEGER DeviceOffset; 8 byte
  ULONG DumpData[1]      ; 4 byte + 4 byte for alignment
} IO_ERROR_LOG_PACKET, *PIO_ERROR_LOG_PACKET;

; total = 48 byte

EDIT: to guarantee that DeviceOffset is 8-byte aligned, it’s not enough for the member to be aligned within the struct, but also for the struct to be aligned to 8-bytes. Therefore the struct is also 8-byte aligned, and consequently padded to 48 bytes. (Otherwise in an array of such structs, say, DeviceOffset within every other element would be misaligned).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading