算法旅人
2021年12月10日
这次的MP依旧来者不善,他是我们的倒数第二个MP,是上次MP9的延续,这次需要我们去思考更大的地图,去动态分配我们的算法所需要的空间,这样才不会core dumped
Mieber: Walk Me There, Part II
Your task this week is to extend last week’s program to make use of dynamic allocation in several ways and to attempt to pair up requests from a list of many requests. For this purpose, you must also implement and use structures designed to help improve performance. In particular, you must write C subroutines that manage dynamic allocation of vertex sets and paths, subroutines that enable, calculate, and use “mini maps” based on a high level of a pyramid tree, and a subroutine that attempts to find a partner for a request among a linked list of unpaired requests. Successful pairings must then be moved into a list of paired requests. The objective for this week is for you to gain experience with dynamic allocation and singly-linked lists in C.
老规矩,看过需求文档和头文件后,我们先写函数头说明,再写新知识对应的函数和框架
1, Link-List
链表是一种很好的数据结构,能同态分配我们需要的函数空间,这里就首先需要知道怎么使用melloc 和alloc 了,这篇文章就写的很棒
[C语言] 5分钟看懂什么是 malloc - 知乎 (zhihu.com)https://zhuanlan.zhihu.com/p/105090421MP10的链表主要用在request match上,因为有几十条request 需要互相查找,链接,所以我们用link list 是不错的想法(吗
以及吐槽一句,python 自然能分配空间给list 不香吗,🐕都不用C
2,Malloc
研究如何使用的tool program:
#include<stdlib.h>
#include<stdio.h>
int main(void) {
int i = 0;
int N = 0;
int* arr;
printf("请输入数组的大小\n");
scanf("%d", &N);
arr = (int*)malloc(sizeof(int) * (N-1));
printf("请输入%d个数\n", N);
for (i = 0; i < N; i++)
{
scanf("%d", &arr[i]);
char c = char(arr);
printf("%c",c);
printf("成功输出arr[%d] = %d\n",i,arr[i]);
}
free(arr);
return 0;
}
一个有趣的可以检测自己电脑有多少内存的程序,我怀疑调用的是C盘的资源
#include<stdlib.h>
#include<stdio.h>
int main(void) {
void* p;
int i = 0;
//每次申请100M,失败返回空指针0,退出循环
while ((p = malloc(1024 * 1024 * 100)))
i++;
printf("最多分配%d00M内存", i);
return 0;
}
5,