If you tell me the compiler directives to force stuff to be aligned, I'll try adding them in to (hopefully) the appropriate places.
#include <stdio.h> #include <stdlib.h> #include <stddef.h> #pragma pack(1) typedef struct s { char x; int y; } S; #pragma pack() int main() { int i; char *buffer = (char *)malloc(16); for (i = 0; i < 16; i++) buffer[i] = i; printf("reading unaligned value from the buffer at offset 1: %08X\n", *(int *)(buffer + 1)); printf("offsetof(S, y)=%d\n", offsetof(S, y)); printf("sizeof(S)=%d\n", sizeof(S)); free(buffer); return 0; }
reading unaligned value from the buffer at offset 1: 04030201 offsetof(S, y)=1 sizeof(S)=5