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 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 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 0000000000000000000000000000000000000000..568fde11c6fef630b0b55ce7b391034a09e458de --- /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 fd6411087b338855c7247f83278d7ca615cc56c6..def4df50d5e3b547fb96810db928d71f6cde714f 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;