作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
#include<stdio.h>
#include<stdlib.h>
typedef struct _anyT {
int val;
} anyT;
#define UNITS 10
typedef union _data_block {
union _data_block *next;
anyT the_type;
} data_block;
static data_block *free_list = NULL;
static void moremem(void) {
int i;
data_block *more = calloc(sizeof(data_block),UNITS);
for(i = 0; i < UNITS; i++) {
more[i].next = free_list;
free_list = more + i;
}
}
anyT *allocanyT(void) {
data_block *current;
if(free_list == NULL) {
moremem();
return allocanyT();
}
current = free_list;
free_list = free_list->next;
return &(current->the_type);
}
void freeanyT(anyT *x)
{
((data_block *)x)->next = free_list;
free_list = (data_block *)x;
}
void clearpool() {
data_block *head;
anyT* cur;
for(head=free_list;head;head=free_list) {
free_list=free_list->next;
cur=(anyT*)&head->the_type;
free(cur);
}
}
如上面的代码所示,函数clearpool()实际上不起作用!我想知道如果有专家可以解释我为什么并帮助我解决它。预先感谢您。
最诚挚的问候,
最佳答案
我认为您想要一个结构,而不是 _data_block 的 union 。递归 union 让我的头翻了个底朝天。您还有更多问题,但是这一行:
cur=(anyT*)&head->the_type;
表明您认为 &head->the_type
与 &head->next
不同,但事实并非如此。
关于c - 如何用 C 语言编写清理内存池的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3452988/
我是一名优秀的程序员,十分优秀!