提交 a0c19017 编写于 作者: L luxin

add 2 exercises

上级 9f497aae
{
"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
#include <stdio.h>
#include <stdlib.h>
#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
{
"type": "code_options",
"author": "卢昕",
"source": "memory_management.md",
"exercise_id":"a714745a7e4d4543a90483a3be3a0f73"
}
\ No newline at end of file
# 内存动态管理
使用动态分配内存的方式,模拟C++中Vector容器,实现动态数组的构建。请选出正确答案。
## 答案
```c
#include <stdio.h>
#include <stdlib.h>
#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 <stdio.h>
#include <stdlib.h>
#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 <stdio.h>
#include <stdlib.h>
#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 <stdio.h>
#include <stdlib.h>
#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;
}
```
{
"node_id": "e75bdc3470954cb38bdfcf4f0ed3680b",
"keywords": [
"存储管理",
"内存管理",
"C语言"
],
"children": [],
"export": []
}
\ No newline at end of file
{ {
"node_id": "77363abd916c4981b595f44138f53e0a", "node_id": "77363abd916c4981b595f44138f53e0a",
"keywords": [ "keywords": [
"标准函数库", "标准函数库",
"标准库", "标准库",
"C语言" "C语言"
], ],
"children": [], "children": [],
"export": [] "export": [
"math.json"
]
} }
\ No newline at end of file
{
"type": "code_options",
"author": "卢昕",
"source": "math.md",
"exercise_id":"f764910c876044a3ba3d538a76520386"
}
\ No newline at end of file
#
## 答案
```c
```
## 选项
### A
```c
```
### B
```c
```
### C
```c
```
...@@ -6,5 +6,5 @@ ...@@ -6,5 +6,5 @@
"C语言" "C语言"
], ],
"children": [], "children": [],
"export": [] "export": ["qsort.json"]
} }
\ No newline at end of file
{
"type": "code_options",
"author": "卢昕",
"source": "qsort.md",
"exercise_id":"902f13f3450445e9b2fa9cb99d78a2b8"
}
\ No newline at end of file
#
## 答案
```c
```
## 选项
### A
```c
```
### B
```c
```
### C
```c
```
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#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
{
"type": "code_options",
"author": "卢昕",
"source": "assert.md",
"exercise_id":"4f37695a19624483b4f9e0b694bc3723"
}
\ No newline at end of file
# 安全问题的检查
请从下列选项中选出存在潜在安全问题的程序。
## 答案
```c
#include <stdio.h>
#include <stdlib.h>
#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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#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 <stdio.h>
#include <stdlib.h>
#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 <stdio.h>
#include <stdlib.h>
#include <assert.h>
#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;
}
```
...@@ -6,5 +6,7 @@ ...@@ -6,5 +6,7 @@
"C语言" "C语言"
], ],
"children": [], "children": [],
"export": [] "export": [
"assert.json"
]
} }
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册