From 66bc3d19c451aad80418b5a8fdef6023ae93a5c5 Mon Sep 17 00:00:00 2001 From: tTh Date: Wed, 27 Mar 2024 11:05:42 +0100 Subject: [PATCH] add code/C/packtest.c --- code/C/.gitignore | 1 + code/C/Makefile | 6 ++- code/C/packtest.c | 116 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 122 insertions(+), 1 deletion(-) create mode 100755 code/C/packtest.c diff --git a/code/C/.gitignore b/code/C/.gitignore index 67fa61a..9daed3e 100644 --- a/code/C/.gitignore +++ b/code/C/.gitignore @@ -1,2 +1,3 @@ fgets-simple demo-strtok +packtest diff --git a/code/C/Makefile b/code/C/Makefile index 03b078c..5f67beb 100644 --- a/code/C/Makefile +++ b/code/C/Makefile @@ -2,7 +2,7 @@ # exemples pour le chapitre sur le C # new Sat Feb 11 12:06:34 CET 2023 -all: no-op slowprint fgets-simple +all: no-op slowprint fgets-simple packtest no-op: no-op.c Makefile gcc -Wall $< -o $@ @@ -15,3 +15,7 @@ fgets-simple: fgets-simple.c Makefile demo-strtok: demo-strtok.c Makefile gcc -Wall $< -o $@ + +# added Wed Mar 27 10:01:41 UTC 2024 +packtest: packtest.c + gcc -Wall $< -o $@ diff --git a/code/C/packtest.c b/code/C/packtest.c new file mode 100755 index 0000000..2052de3 --- /dev/null +++ b/code/C/packtest.c @@ -0,0 +1,116 @@ +#include +#include + +/* + * found in the good old Usenet + */ + +/* The expected sizes in these comments assime a 64-bit machine */ + +struct foo1 { + char *p; + char c; + long x; +}; + +struct foo2 { + char c; /* 1 byte */ + char pad[7]; /* 7 bytes */ + char *p; /* 8 bytes */ + long x; /* 8 bytes */ +}; + +struct foo3 { + char *p; /* 8 bytes */ + char c; /* 1 byte */ +}; + +struct foo4 { + short s; /* 2 bytes */ + char c; /* 1 byte */ +}; + +struct foo5 { + char c; + struct foo5_inner { + char *p; + short x; + } inner; +}; + +struct foo6 { + short s; + char c; + int flip:1; + int nybble:4; + int septet:7; +}; + +struct foo7 { + int bigfield:31; + int littlefield:1; +}; + +struct foo8 { + int bigfield1:31; + int littlefield1:1; + int bigfield2:31; + int littlefield2:1; +}; + +struct foo9 { + int bigfield1:31; + int bigfield2:31; + int littlefield1:1; + int littlefield2:1; +}; + +struct foo10 { + char c; + struct foo10 *p; + short x; +}; + +struct foo11 { + struct foo11 *p; + short x; + char c; +}; + +struct foo12 { + struct foo12_inner { + char *p; + short x; + } inner; + char c; +}; + +int main(int argc, char *argv[]) +{ + printf("sizeof(char *) = %zu\n", sizeof(char *)); + printf("sizeof(long) = %zu\n", sizeof(long)); + printf("sizeof(int) = %zu\n", sizeof(int)); + printf("sizeof(short) = %zu\n", sizeof(short)); + printf("sizeof(char) = %zu\n", sizeof(char)); + printf("sizeof(float) = %zu\n", sizeof(float)); + printf("sizeof(double) = %zu\n", sizeof(double)); + printf("sizeof(struct foo1) = %zu\n", sizeof(struct foo1)); + printf("sizeof(struct foo2) = %zu\n", sizeof(struct foo2)); + printf("sizeof(struct foo3) = %zu\n", sizeof(struct foo3)); + printf("sizeof(struct foo4) = %zu\n", sizeof(struct foo4)); + printf("sizeof(struct foo5) = %zu\n", sizeof(struct foo5)); + printf("sizeof(struct foo6) = %zu\n", sizeof(struct foo6)); + printf("sizeof(struct foo7) = %zu\n", sizeof(struct foo7)); + printf("sizeof(struct foo8) = %zu\n", sizeof(struct foo8)); + printf("sizeof(struct foo9) = %zu\n", sizeof(struct foo9)); + printf("sizeof(struct foo10) = %zu\n", sizeof(struct foo10)); + printf("sizeof(struct foo11) = %zu\n", sizeof(struct foo11)); + printf("sizeof(struct foo12) = %zu\n", sizeof(struct foo12)); + + if (sizeof(struct foo3) == 16) { + puts("This looks like a 64-bit machine."); + } else if (sizeof(struct foo3) == 6) { + puts("This looks like a 32-bit machine."); + } + +}