From e54a610af51dde22803909d03c7066b6ddd6fea7 Mon Sep 17 00:00:00 2001 From: feilong Date: Sat, 23 Oct 2021 20:29:16 +0800 Subject: [PATCH] add stucture pack --- .../pack01.json" | 0 .../pack01.md" | 52 +++++++++++++++++++ .../file_hash.c" | 9 +++- 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/7.\345\257\271\351\275\220\347\211\271\346\200\247/pack01.json" create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/7.\345\257\271\351\275\220\347\211\271\346\200\247/pack01.md" diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/7.\345\257\271\351\275\220\347\211\271\346\200\247/pack01.json" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/7.\345\257\271\351\275\220\347\211\271\346\200\247/pack01.json" new file mode 100644 index 0000000..e69de29 diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/7.\345\257\271\351\275\220\347\211\271\346\200\247/pack01.md" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/7.\345\257\271\351\275\220\347\211\271\346\200\247/pack01.md" new file mode 100644 index 0000000..568fde1 --- /dev/null +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/7.\345\257\271\351\275\220\347\211\271\346\200\247/pack01.md" @@ -0,0 +1,52 @@ +# 数的结构判断 + +假设机器字是8 bytes,下面代码的占据的内存大小是多大? +```c +struct Test{ + char *p; + char c; + int x; +} +``` + +更多知识请参考:[失传的C结构体打包技艺](https://github.com/ludx/The-Lost-Art-of-C-Structure-Packing) + +## template + +```c +struct Test{ + char *p; + char c; + int x; +}; +int main(int argc, char**){ + print("%d", sizeof(struct Test)) + return 0; +} +``` + +## 答案 + +```c +8 bytes +``` + +## 选项 + +### 选项1 + +```c +6 bytes +``` + +### 选项2 + +```c +9 bytes +``` + +### 选项3 + +```c +24 bytes +``` \ No newline at end of file diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/5.\346\226\207\344\273\266/2.\346\226\207\344\273\266\347\232\204\350\257\273\345\206\231/file_hash.c" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/5.\346\226\207\344\273\266/2.\346\226\207\344\273\266\347\232\204\350\257\273\345\206\231/file_hash.c" index fd64110..def4df5 100644 --- "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/5.\346\226\207\344\273\266/2.\346\226\207\344\273\266\347\232\204\350\257\273\345\206\231/file_hash.c" +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/5.\346\226\207\344\273\266/2.\346\226\207\344\273\266\347\232\204\350\257\273\345\206\231/file_hash.c" @@ -182,7 +182,14 @@ void sha256_read_hex(const struct sha256_buff *buff, char *hex) { } int main(int argc, char **argv) { - FILE *file = fopen(argv[1], "rb"); + + FILE *f1 = fopen("/tmp/c_hash_test.txt", "w+"); + for (int i = 0; i < 1024; i++) { + fputs("Hello,World!", f1); + } + fclose(f1); + + FILE *file = fopen("/tmp/c_hash_test.txt", "rb"); if (!file) { printf("Cannot open file\n"); return 0; -- GitLab