I'm slightly confused as to how much a uint8_t occupies when using the MSVC compiler. Also, I'm somewhat familiar with concept of struct padding such memory is aligned for efficient read/writes. However, my tests show some weird results. I define three structs:
The first features a uint8 and int32. I would expect this to occupy 8 bytes, since the int32 must be word aligned forcing 3 padding bytes to be added. I am correct in my assumption.
The second features a single uint8. I would have expected this to occupy 4 bytes.. but instead it only occupies 1. This kind of confuses me.
The third (and most confusing one) features an int32 followed by a uint8. Using the logic from struct 2 where a lone uint8 occupies a single byte, I would have assumed this struct to occupy 5 bytes. But it occupies 8 bytes. This kind of makes sense, but it doesn't make sense then that struct 2 would only occupy 1 byte.
How much space does uint8 actually occupy?
typedef struct StructOne
{
uint8_t member1;
int32_t member2;
} StructOne;
typedef struct StructTwo
{
uint8_t member1;
} StructTwo;
typedef struct StructThree
{
int32_t member2;
uint8_t member1;
} StructThree;
int main(int argc, char* args[])
{
size_t size_struct_one = sizeof(StructOne);
size_t size_struct_two = sizeof(StructTwo);
size_t size_struct_three = sizeof(StructThree);
printf("Size of StructOne = %u\n", sizeof(StructOne));
printf("Size of StructTwo = %u\n", sizeof(StructTwo));
printf("Size of StructThree = %u\n", sizeof(StructThree));
return 0;
}