From a0c190173647e1f4392993abe9a78b82c2021280 Mon Sep 17 00:00:00 2001 From: luxin Date: Sun, 24 Oct 2021 09:15:32 +0800 Subject: [PATCH] add 2 exercises --- .../config.json" | 34 +++ .../memory_management.c" | 54 ++++ .../memory_management.json" | 6 + .../memory_management.md" | 239 ++++++++++++++++++ .../config.json" | 10 - .../config.json" | 18 +- .../math.c" | 0 .../math.json" | 6 + .../math.md" | 23 ++ .../config.json" | 2 +- .../qsort.c" | 0 .../qsort.json" | 6 + .../qsort.md" | 23 ++ .../assert.c" | 23 ++ .../assert.json" | 6 + .../assert.md" | 130 ++++++++++ .../config.json" | 4 +- 17 files changed, 564 insertions(+), 20 deletions(-) create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/6.\345\255\230\345\202\250\347\256\241\347\220\206/2.\345\206\205\345\255\230\345\212\250\346\200\201\347\256\241\347\220\206/config.json" create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/6.\345\255\230\345\202\250\347\256\241\347\220\206/2.\345\206\205\345\255\230\345\212\250\346\200\201\347\256\241\347\220\206/memory_management.c" create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/6.\345\255\230\345\202\250\347\256\241\347\220\206/2.\345\206\205\345\255\230\345\212\250\346\200\201\347\256\241\347\220\206/memory_management.json" create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/6.\345\255\230\345\202\250\347\256\241\347\220\206/2.\345\206\205\345\255\230\345\212\250\346\200\201\347\256\241\347\220\206/memory_management.md" delete mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/6.\345\255\230\345\202\250\347\256\241\347\220\206/2.\345\210\206\351\205\215\345\206\205\345\255\230\357\274\232malloc()\345\222\214free()/config.json" create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/1.\346\225\260\345\255\246\345\272\223/math.c" create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/1.\346\225\260\345\255\246\345\272\223/math.json" create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/1.\346\225\260\345\255\246\345\272\223/math.md" create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/2.\351\200\232\347\224\250\345\267\245\345\205\267\345\272\223/qsort.c" create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/2.\351\200\232\347\224\250\345\267\245\345\205\267\345\272\223/qsort.json" create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/2.\351\200\232\347\224\250\345\267\245\345\205\267\345\272\223/qsort.md" create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/3.\346\226\255\350\250\200\345\272\223/assert.c" create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/3.\346\226\255\350\250\200\345\272\223/assert.json" create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/3.\346\226\255\350\250\200\345\272\223/assert.md" rename "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/3.\346\226\255\350\250\200\345\272\223 /config.json" => "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/3.\346\226\255\350\250\200\345\272\223/config.json" (79%) diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/6.\345\255\230\345\202\250\347\256\241\347\220\206/2.\345\206\205\345\255\230\345\212\250\346\200\201\347\256\241\347\220\206/config.json" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/6.\345\255\230\345\202\250\347\256\241\347\220\206/2.\345\206\205\345\255\230\345\212\250\346\200\201\347\256\241\347\220\206/config.json" new file mode 100644 index 0000000..c7f85ba --- /dev/null +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/6.\345\255\230\345\202\250\347\256\241\347\220\206/2.\345\206\205\345\255\230\345\212\250\346\200\201\347\256\241\347\220\206/config.json" @@ -0,0 +1,34 @@ +{ + "node_id": "e75bdc3470954cb38bdfcf4f0ed3680b", + "keywords": [ + "存储管理", + "内存管理", + "内存动态管理", + "C语言" + ], + "children": [ + { + "内存分配:malloc()": { + "keywords": [ + "内存分配", + "malloc", + "C语言" + ], + "children": [] + } + }, + { + "内存释放:free()": { + "keywords": [ + "内存释放", + "free", + "C语言" + ], + "children": [] + } + } + ], + "export": [ + "memory_management.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/6.\345\255\230\345\202\250\347\256\241\347\220\206/2.\345\206\205\345\255\230\345\212\250\346\200\201\347\256\241\347\220\206/memory_management.c" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/6.\345\255\230\345\202\250\347\256\241\347\220\206/2.\345\206\205\345\255\230\345\212\250\346\200\201\347\256\241\347\220\206/memory_management.c" new file mode 100644 index 0000000..d1dc5f0 --- /dev/null +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/6.\345\255\230\345\202\250\347\256\241\347\220\206/2.\345\206\205\345\255\230\345\212\250\346\200\201\347\256\241\347\220\206/memory_management.c" @@ -0,0 +1,54 @@ +#include +#include + +#define N 5 + +int *readline(int *mem_size) +{ + int *array = NULL; + int size = N; + array = (int *)malloc((size + 1) * sizeof(int)); + if (array == NULL) + { + printf("内存分配失败!\n"); + exit(-1); + } + + int value; + int count = 0; + printf("请输入一串整数(以空格隔开)\n(Win按Ctrl+Z结束)\n(Mac先按回车,再按Ctrl+D结束):\n"); + while (scanf("%d", &value) != EOF) + { + ++count; + if (count > size) + { + size += N; + array = realloc(array, (size + 1) * sizeof(int)); + if (array == NULL) + { + printf("重新分配内存空间失败!\n"); + exit(-1); + } + } + + array[count] = value; + } + array[0] = count; + *mem_size = size; + return array; +} + +int main() +{ + int i, mem_size, count; + int *array = readline(&mem_size); + count = array[0]; + printf("------------------------------\n"); + printf("内存空间大小为:%d\n实际使用量为:%d\n", mem_size, count + 1); + printf("实际存储数据为:"); + for (i = 1; i <= count; ++i) + printf("%d ", array[i]); + free(array); + + 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/6.\345\255\230\345\202\250\347\256\241\347\220\206/2.\345\206\205\345\255\230\345\212\250\346\200\201\347\256\241\347\220\206/memory_management.json" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/6.\345\255\230\345\202\250\347\256\241\347\220\206/2.\345\206\205\345\255\230\345\212\250\346\200\201\347\256\241\347\220\206/memory_management.json" new file mode 100644 index 0000000..68ebb47 --- /dev/null +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/6.\345\255\230\345\202\250\347\256\241\347\220\206/2.\345\206\205\345\255\230\345\212\250\346\200\201\347\256\241\347\220\206/memory_management.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "卢昕", + "source": "memory_management.md", + "exercise_id":"a714745a7e4d4543a90483a3be3a0f73" +} \ No newline at end of file diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/6.\345\255\230\345\202\250\347\256\241\347\220\206/2.\345\206\205\345\255\230\345\212\250\346\200\201\347\256\241\347\220\206/memory_management.md" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/6.\345\255\230\345\202\250\347\256\241\347\220\206/2.\345\206\205\345\255\230\345\212\250\346\200\201\347\256\241\347\220\206/memory_management.md" new file mode 100644 index 0000000..7bd0325 --- /dev/null +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/6.\345\255\230\345\202\250\347\256\241\347\220\206/2.\345\206\205\345\255\230\345\212\250\346\200\201\347\256\241\347\220\206/memory_management.md" @@ -0,0 +1,239 @@ +# 内存动态管理 + +使用动态分配内存的方式,模拟C++中Vector容器,实现动态数组的构建。请选出正确答案。 + +## 答案 +```c +#include +#include + +#define N 5 + +int *readline(int *mem_size) +{ + int *array = NULL; + int size = N; + array = (int *)malloc((size + 1) * sizeof(int)); + if (array == NULL) + { + printf("内存分配失败!\n"); + exit(-1); + } + + int value; + int count = 0; + printf("请输入一串整数(以空格隔开)\n(Win按Ctrl+Z结束)\n(Mac先按回车,再按Ctrl+D结束):\n"); + while (scanf("%d", &value) != EOF) + { + ++count; + if (count > size) + { + size += N; + array = realloc(array, (size + 1) * sizeof(int)); + if (array == NULL) + { + printf("重新分配内存空间失败!\n"); + exit(-1); + } + } + + array[count] = value; + } + array[0] = count; + *mem_size = size; + return array; +} + +int main() +{ + int i, mem_size, count; + int *array = readline(&mem_size); + count = array[0]; + printf("------------------------------\n"); + printf("内存空间大小为:%d\n实际使用量为:%d\n", mem_size, count + 1); + printf("实际存储数据为:"); + for (i = 1; i <= count; ++i) + printf("%d ", array[i]); + free(array); + + return 0; +} +``` + +## 选项 +### A +```c +#include +#include + +#define N 5 + +int *readline(int *mem_size) +{ + int *array = NULL; + int size = N; + array = (int *)malloc((size + 1) * sizeof(int)); + if (array == NULL) + { + printf("内存分配失败!\n"); + exit(-1); + } + + int value; + int count = 0; + printf("请输入一串整数(以空格隔开)\n(Win按Ctrl+Z结束)\n(Mac先按回车,再按Ctrl+D结束):\n"); + while (scanf("%d", &value) != EOF) + { + ++count; + if (count > size) + { + size += N; + array = realloc(array, (size + 1) * sizeof(int)); + if (array == NULL) + { + printf("重新分配内存空间失败!\n"); + exit(-1); + } + } + + array[count] = value; + } + array[0] = count; + *mem_size = size + 1; + free(array); + return array; +} + +int main() +{ + int i, mem_size, count; + int *array = readline(&mem_size); + count = array[0]; + printf("------------------------------\n"); + printf("内存空间大小为:%d\n实际使用量为:%d\n", mem_size, count + 1); + printf("实际存储数据为:"); + for (i = 1; i <= count; ++i) + printf("%d ", array[i]); + + return 0; +} +``` + +### B +```c +#include +#include + +#define N 5 + +int *readline(int *mem_size) +{ + int *array = NULL; + int size = N; + array = (int *)malloc((size + 1) * sizeof(int)); + if (array == NULL) + { + printf("内存分配失败!\n"); + exit(-1); + } + + int value; + int count = 0; + printf("请输入一串整数(以空格隔开)\n(Win按Ctrl+Z结束)\n(Mac先按回车,再按Ctrl+D结束):\n"); + while (scanf("%d", &value) != EOF) + { + ++count; + if (count > size) + { + size += N; + array = realloc(array, (size + 1) * sizeof(int)); + if (array == NULL) + { + printf("重新分配内存空间失败!\n"); + exit(-1); + } + } + + array[count] = value; + } + array[0] = count; + *mem_size = size + 1; + return array; +} + +int main() +{ + int i, mem_size, count; + int *array = readline(&mem_size); + count = array[0]; + printf("------------------------------\n"); + printf("内存空间大小为:%d\n实际使用量为:%d\n", mem_size, count + 1); + printf("实际存储数据为:"); + for (i = 1; i <= count; ++i) + { + printf("%d ", array[i]); + free(&array[i]); + } + free(array); + + return 0; +} +``` + +### C +```c +#include +#include + +#define N 5 + +int *readline(int *mem_size) +{ + int *array = NULL; + int size = N; + array = malloc(size + 1); + if (array == NULL) + { + printf("内存分配失败!\n"); + exit(-1); + } + + int value; + int count = 0; + printf("请输入一串整数(以空格隔开)\n(Win按Ctrl+Z结束)\n(Mac先按回车,再按Ctrl+D结束):\n"); + while (scanf("%d", &value) != EOF) + { + ++count; + if (count > size) + { + size += N; + array = realloc(array, (size + 1) * sizeof(int)); + if (array == NULL) + { + printf("重新分配内存空间失败!\n"); + exit(-1); + } + } + + array[count] = value; + } + array[0] = count; + *mem_size = size + 1; + return array; +} + +int main() +{ + int i, mem_size, count; + int *array = readline(&mem_size); + count = array[0]; + printf("------------------------------\n"); + printf("内存空间大小为:%d\n实际使用量为:%d\n", mem_size, count + 1); + printf("实际存储数据为:"); + for (i = 1; i <= count; ++i) + printf("%d ", array[i]); + free(array); + + return 0; +} +``` diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/6.\345\255\230\345\202\250\347\256\241\347\220\206/2.\345\210\206\351\205\215\345\206\205\345\255\230\357\274\232malloc()\345\222\214free()/config.json" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/6.\345\255\230\345\202\250\347\256\241\347\220\206/2.\345\210\206\351\205\215\345\206\205\345\255\230\357\274\232malloc()\345\222\214free()/config.json" deleted file mode 100644 index 93afac5..0000000 --- "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/6.\345\255\230\345\202\250\347\256\241\347\220\206/2.\345\210\206\351\205\215\345\206\205\345\255\230\357\274\232malloc()\345\222\214free()/config.json" +++ /dev/null @@ -1,10 +0,0 @@ -{ - "node_id": "e75bdc3470954cb38bdfcf4f0ed3680b", - "keywords": [ - "存储管理", - "内存管理", - "C语言" - ], - "children": [], - "export": [] -} \ No newline at end of file diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/1.\346\225\260\345\255\246\345\272\223/config.json" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/1.\346\225\260\345\255\246\345\272\223/config.json" index 57589c2..406a226 100644 --- "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/1.\346\225\260\345\255\246\345\272\223/config.json" +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/1.\346\225\260\345\255\246\345\272\223/config.json" @@ -1,10 +1,12 @@ { - "node_id": "77363abd916c4981b595f44138f53e0a", - "keywords": [ - "标准函数库", - "标准库", - "C语言" - ], - "children": [], - "export": [] + "node_id": "77363abd916c4981b595f44138f53e0a", + "keywords": [ + "标准函数库", + "标准库", + "C语言" + ], + "children": [], + "export": [ + "math.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/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/1.\346\225\260\345\255\246\345\272\223/math.c" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/1.\346\225\260\345\255\246\345\272\223/math.c" 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/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/1.\346\225\260\345\255\246\345\272\223/math.json" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/1.\346\225\260\345\255\246\345\272\223/math.json" new file mode 100644 index 0000000..582c376 --- /dev/null +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/1.\346\225\260\345\255\246\345\272\223/math.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "卢昕", + "source": "math.md", + "exercise_id":"f764910c876044a3ba3d538a76520386" +} \ No newline at end of file diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/1.\346\225\260\345\255\246\345\272\223/math.md" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/1.\346\225\260\345\255\246\345\272\223/math.md" new file mode 100644 index 0000000..19807a9 --- /dev/null +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/1.\346\225\260\345\255\246\345\272\223/math.md" @@ -0,0 +1,23 @@ +# + + +## 答案 +```c + +``` + +## 选项 +### A +```c + +``` + +### B +```c + +``` + +### C +```c + +``` diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/2.\351\200\232\347\224\250\345\267\245\345\205\267\345\272\223/config.json" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/2.\351\200\232\347\224\250\345\267\245\345\205\267\345\272\223/config.json" index 4f5adb0..40aac58 100644 --- "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/2.\351\200\232\347\224\250\345\267\245\345\205\267\345\272\223/config.json" +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/2.\351\200\232\347\224\250\345\267\245\345\205\267\345\272\223/config.json" @@ -6,5 +6,5 @@ "C语言" ], "children": [], - "export": [] + "export": ["qsort.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/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/2.\351\200\232\347\224\250\345\267\245\345\205\267\345\272\223/qsort.c" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/2.\351\200\232\347\224\250\345\267\245\345\205\267\345\272\223/qsort.c" 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/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/2.\351\200\232\347\224\250\345\267\245\345\205\267\345\272\223/qsort.json" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/2.\351\200\232\347\224\250\345\267\245\345\205\267\345\272\223/qsort.json" new file mode 100644 index 0000000..c32daca --- /dev/null +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/2.\351\200\232\347\224\250\345\267\245\345\205\267\345\272\223/qsort.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "卢昕", + "source": "qsort.md", + "exercise_id":"902f13f3450445e9b2fa9cb99d78a2b8" +} \ No newline at end of file diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/2.\351\200\232\347\224\250\345\267\245\345\205\267\345\272\223/qsort.md" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/2.\351\200\232\347\224\250\345\267\245\345\205\267\345\272\223/qsort.md" new file mode 100644 index 0000000..19807a9 --- /dev/null +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/2.\351\200\232\347\224\250\345\267\245\345\205\267\345\272\223/qsort.md" @@ -0,0 +1,23 @@ +# + + +## 答案 +```c + +``` + +## 选项 +### A +```c + +``` + +### B +```c + +``` + +### C +```c + +``` diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/3.\346\226\255\350\250\200\345\272\223/assert.c" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/3.\346\226\255\350\250\200\345\272\223/assert.c" new file mode 100644 index 0000000..2f62482 --- /dev/null +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/3.\346\226\255\350\250\200\345\272\223/assert.c" @@ -0,0 +1,23 @@ +#include +#include +#include +#define N 10 + +void *mem_cpy(void *dest, const void *src, size_t len) +{ + assert(dest != NULL && src !=NULL); + char *tmp_dest = (char *)dest; + char *tmp_src = (char *)src; + while(len --) + *tmp_dest ++ = *tmp_src ++; + return dest; +} + +int main() +{ + int *dest = NULL; + int *src = (int *)malloc(N * sizeof(int)); + dest = mem_cpy(dest, src, N); + + 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/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/3.\346\226\255\350\250\200\345\272\223/assert.json" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/3.\346\226\255\350\250\200\345\272\223/assert.json" new file mode 100644 index 0000000..3acab5c --- /dev/null +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/3.\346\226\255\350\250\200\345\272\223/assert.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "卢昕", + "source": "assert.md", + "exercise_id":"4f37695a19624483b4f9e0b694bc3723" +} \ No newline at end of file diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/3.\346\226\255\350\250\200\345\272\223/assert.md" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/3.\346\226\255\350\250\200\345\272\223/assert.md" new file mode 100644 index 0000000..59720d3 --- /dev/null +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/3.\346\226\255\350\250\200\345\272\223/assert.md" @@ -0,0 +1,130 @@ +# 安全问题的检查 + +请从下列选项中选出存在潜在安全问题的程序。 + +## 答案 +```c +#include +#include +#define N 10 + +void *mem_cpy(void *dest, const void *src, size_t len) +{ + char *tmp_dest = (char *)dest; + char *tmp_src = (char *)src; + while(len --) + *tmp_dest ++ = *tmp_src ++; + return dest; +} + +int main() +{ + int *dest = NULL; + int *src = (int *)malloc(N * sizeof(int)); + dest = mem_cpy(dest, src, N); + + return 0; +} +``` + +## 选项 +### A +```c +#include +#include +#include +#define N 10 + +void *mem_cpy(void *dest, const void *src, size_t len) +{ + if(dest == NULL) + { + fprintf(stderr,"dest is NULL\n"); + abort(); + } + if(src == NULL) + { + fprintf(stderr,"src is NULL\n"); + abort(); + } + char *tmp_dest = (char *)dest; + char *tmp_src = (char *)src; + while(len --) + *tmp_dest ++ = *tmp_src ++; + return dest; +} + +int main() +{ + int *dest = NULL; + int *src = (int *)malloc(N * sizeof(int)); + dest = mem_cpy(dest, src, N); + + return 0; +} +``` + +### B +```c +#include +#include +#define N 10 +#define DEBUG + +void *mem_cpy(void *dest, const void *src, size_t len) +{ + #ifdef DEBUG + if(dest == NULL) + { + fprintf(stderr,"dest is NULL\n"); + abort(); + } + if(src == NULL) + { + fprintf(stderr,"src is NULL\n"); + abort(); + } + #endif + char *tmp_dest = (char *)dest; + char *tmp_src = (char *)src; + while(len --) + *tmp_dest ++ = *tmp_src ++; + return dest; +} + +int main() +{ + int *dest = NULL; + int *src = (int *)malloc(N * sizeof(int)); + dest = mem_cpy(dest, src, N); + + return 0; +} +``` + +### C +```c +#include +#include +#include +#define N 10 + +void *mem_cpy(void *dest, const void *src, size_t len) +{ + assert(dest != NULL && src !=NULL); + char *tmp_dest = (char *)dest; + char *tmp_src = (char *)src; + while(len --) + *tmp_dest ++ = *tmp_src ++; + return dest; +} + +int main() +{ + int *dest = NULL; + int *src = (int *)malloc(N * sizeof(int)); + dest = mem_cpy(dest, src, N); + + return 0; +} +``` diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/3.\346\226\255\350\250\200\345\272\223 /config.json" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/3.\346\226\255\350\250\200\345\272\223/config.json" similarity index 79% rename from "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/3.\346\226\255\350\250\200\345\272\223 /config.json" rename to "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/3.\346\226\255\350\250\200\345\272\223/config.json" index 8befa03..2ffc451 100644 --- "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/3.\346\226\255\350\250\200\345\272\223 /config.json" +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/7.\346\240\207\345\207\206\345\207\275\346\225\260\345\272\223/3.\346\226\255\350\250\200\345\272\223/config.json" @@ -6,5 +6,7 @@ "C语言" ], "children": [], - "export": [] + "export": [ + "assert.json" + ] } \ No newline at end of file -- GitLab