Skip to content

Instantly share code, notes, and snippets.

@nddrylliog
Created November 18, 2012 22:17
Show Gist options
  • Save nddrylliog/4107837 to your computer and use it in GitHub Desktop.
Save nddrylliog/4107837 to your computer and use it in GitHub Desktop.
Test struct layout by GCC
#include <stdio.h>
struct s0 {
char c1;
};
struct s1 {
char c1;
char c2;
};
struct s2 {
void *ptr;
char c2;
};
struct s3 {
char c1;
void *ptr;
char c2;
};
struct s4 {
char c1;
long double ld;
char c2;
};
int main (int argc, char **argv) {
printf("size of struct s0 = %lu\n", sizeof(struct s0));
printf("size of struct s1 = %lu\n", sizeof(struct s1));
printf("size of struct s2 = %lu\n", sizeof(struct s2));
printf("size of struct s3 = %lu\n", sizeof(struct s3));
printf("size of struct s4 = %lu\n", sizeof(struct s4));
}
@duckinator
Copy link

@nddrylliog

On my Pi (32bit ARM):

size of struct s0 = 1
size of struct s1 = 2
size of struct s2 = 8
size of struct s3 = 12
size of struct s4 = 24

On my desktop (64bit x86):

size of struct s0 = 1
size of struct s1 = 2
size of struct s2 = 16
size of struct s3 = 24
size of struct s4 = 48

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment