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/config.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/6.\345\257\271\351\275\220\347\211\271\346\200\247/config.json" similarity index 69% rename from "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/config.json" rename to "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/6.\345\257\271\351\275\220\347\211\271\346\200\247/config.json" index 8f39d594ef2f3c58013390bbe6b35f3869f5ee85..0e6521a5b38339b9281de513062f8cd8f5a64f03 100644 --- "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/config.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/6.\345\257\271\351\275\220\347\211\271\346\200\247/config.json" @@ -6,5 +6,8 @@ "C语言" ], "children": [], - "export": [] + "export": [ + "pack01.json", + "pack02.json" + ] } \ No newline at end of file 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/6.\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/6.\345\257\271\351\275\220\347\211\271\346\200\247/pack01.json" new file mode 100644 index 0000000000000000000000000000000000000000..c14c57a35ba0301928c4c9f6dae4d341e28aa13d --- /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/6.\345\257\271\351\275\220\347\211\271\346\200\247/pack01.json" @@ -0,0 +1,5 @@ +{ + "type": "code_options", + "author": "幻灰龙", + "source": "pack01.md" +} \ No newline at end of file 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/6.\345\257\271\351\275\220\347\211\271\346\200\247/pack01.md" similarity index 85% rename from "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" rename to "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/6.\345\257\271\351\275\220\347\211\271\346\200\247/pack01.md" index 96a74b96e7bf254c92b2ad29f8ca8bcd6d95d808..ce7f0e36c00366f7d43e790ee5bb1d415d131fc7 100644 --- "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/6.\345\257\271\351\275\220\347\211\271\346\200\247/pack01.md" @@ -1,4 +1,4 @@ -# 结构体位对齐 +# 结构体位对齐(1) 假设机器字是8 bytes,下面代码的占据的内存大小是多大? ```c @@ -14,13 +14,14 @@ struct Test{ ## template ```c +#include struct Test{ char *p; char c; int x; }; int main(int argc, char**){ - print("%d", sizeof(struct Test)) + printf("%d", sizeof(struct Test)); return 0; } ``` 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/6.\345\257\271\351\275\220\347\211\271\346\200\247/pack02.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/6.\345\257\271\351\275\220\347\211\271\346\200\247/pack02.json" new file mode 100644 index 0000000000000000000000000000000000000000..e603144246e8c9a7a57f9b80c667c07e778e31f1 --- /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/6.\345\257\271\351\275\220\347\211\271\346\200\247/pack02.json" @@ -0,0 +1,5 @@ +{ + "type": "code_options", + "author": "幻灰龙", + "source": "pack02.md" +} \ No newline at end of file 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/6.\345\257\271\351\275\220\347\211\271\346\200\247/pack02.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/6.\345\257\271\351\275\220\347\211\271\346\200\247/pack02.md" new file mode 100644 index 0000000000000000000000000000000000000000..8e4f3a011db0ca23cce851112136eb06a5b15593 --- /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/6.\345\257\271\351\275\220\347\211\271\346\200\247/pack02.md" @@ -0,0 +1,49 @@ +# 结构体位对齐(2) + +结构体需要与其最宽的成员为基准对齐,假设机器字是8 bytes,下面代码的占据的内存大小是多大? +```c +struct foo3 { + char *p; + char c; +}; +``` + +## template + +```c +#include +struct foo3 { + char *p; + char c; +}; +int main(int argc, char**){ + printf("%d", sizeof(struct foo3)); + return 0; +} +``` + +## 答案 + +```c +16 bytes +``` + +## 选项 + +### 选项1 + +```c +9 bytes +``` + +### 选项2 + +```c +12 bytes +``` + +### 选项3 + +```c +8 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/3.\344\275\215\350\277\220\347\256\227/7.\344\275\215\345\255\227\346\256\265/bit_field.c" "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.\344\275\215\345\255\227\346\256\265/bit_field.c" new file mode 100644 index 0000000000000000000000000000000000000000..b6653f886877f19ec2738fddb6b0788493e2f39f --- /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.\344\275\215\345\255\227\346\256\265/bit_field.c" @@ -0,0 +1,10 @@ +#include +struct foo5 { + int flip : 1; + int nybble : 4; + int septet : 7; +}; +int main(int argc, char **argv) { + printf("%lu", sizeof(struct foo5)); + return 0; +} \ No newline at end of file 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.\344\275\215\345\255\227\346\256\265/bit_field.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.\344\275\215\345\255\227\346\256\265/bit_field.json" new file mode 100644 index 0000000000000000000000000000000000000000..7eba2ba2d2d5fd20e66edf0cd8a7e734359819e9 --- /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.\344\275\215\345\255\227\346\256\265/bit_field.json" @@ -0,0 +1,5 @@ +{ + "type": "code_options", + "author": "幻灰龙", + "source": "bit_field.md" +} \ No newline at end of file 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.\344\275\215\345\255\227\346\256\265/bit_field.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.\344\275\215\345\255\227\346\256\265/bit_field.md" new file mode 100644 index 0000000000000000000000000000000000000000..9133d493d7aceb009f7476c41dde0b77fb0b63a6 --- /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.\344\275\215\345\255\227\346\256\265/bit_field.md" @@ -0,0 +1,57 @@ +# 十六进制转换 + +利用位域,你能声明比字符宽度更小的成员,低至1位。假设机器字是8 bytes,下面的结构体大小是? + +```c +#include +struct foo5 { + int flip : 1; + int nybble : 4; + int septet : 7; +}; +int main(int argc, char **argv) { + printf("%lu", sizeof(struct foo5)); + return 0; +} +``` + +## template + +```c +#include +struct foo5 { + int flip : 1; + int nybble : 4; + int septet : 7; +}; +int main(int argc, char**){ + printf("%lu", sizeof(struct foo5)); + return 0; +} +``` + +## 答案 + +```c +4 +``` + +## 选项 + +### 选项1 + +```c +16 +``` + +### 选项2 + +```c +32 +``` + +### 选项3 + +```c +8 +``` \ No newline at end of file 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/6.\344\275\215\345\255\227\346\256\265/config.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.\344\275\215\345\255\227\346\256\265/config.json" similarity index 76% rename from "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/6.\344\275\215\345\255\227\346\256\265/config.json" rename to "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.\344\275\215\345\255\227\346\256\265/config.json" index d31d13ea3799447eb7bf6d92f39d92d03d33c6ef..f1622138208e5978dbf5add4587e67d5dfc8f8e4 100644 --- "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/6.\344\275\215\345\255\227\346\256\265/config.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.\344\275\215\345\255\227\346\256\265/config.json" @@ -6,5 +6,7 @@ "C语言" ], "children": [], - "export": [] + "export": [ + "bit_filed.json" + ] } \ No newline at end of file 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" deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000