ECE220生存指南[05]MP10:动态生成数组空间

算法旅人

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,

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值