Skip to content

Instantly share code, notes, and snippets.

@Costava
Created February 15, 2022 00:10
Show Gist options
  • Save Costava/2a01d3ebdb1f4be0827c752fe79bbaa2 to your computer and use it in GitHub Desktop.
Save Costava/2a01d3ebdb1f4be0827c752fe79bbaa2 to your computer and use it in GitHub Desktop.
// gcc structpadding.c -std=c99 -Wall
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
typedef struct Foo1 {
uint64_t a;
} Foo1;
typedef struct Foo2 {
uint64_t a;
bool b;
} Foo2;
int main(void) {
Foo1 foo1Array[2];
Foo2 foo2Array[2];
printf("sizeof(Foo1): %2ld\n", sizeof(Foo1)); // 8
printf("sizeof(Foo2): %2ld\n", sizeof(Foo2)); // 16 (not 9)
printf("sizeof(bool): %2ld\n", sizeof(bool)); // 1
printf("sizeof(foo1Array): %2ld\n", sizeof(foo1Array)); // 16
printf("sizeof(foo2Array): %2ld\n", sizeof(foo2Array)); // 32
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment