自己写的双向链表法malloc和free,运行结果都是有段错误,求大神指点Orz
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 10000
#define ARR_LENGTH 100
///////////////双向链表法///////////////////////
typedef struct block {
struct block* pre;
struct block* next;
size_t size;
int tag;
}Block;
char start[N];//10000
int has_initialized = 0;//内存未初始化
/*-------------------初始化---------------------------*/
void mymalloc_init() {
Block *memory_start =(Block*) start;
memory_start->pre = NULL;
memory_start->next = NULL;
memory_start->size = N - sizeof(Block);
memory_start->tag = 0;
has_initialized = 1;//表示已经初始化
return;
}
/*-------------------malloc最先法---------------------------*/
void* mymalloc_first(size_t size){
if(!has_initialized)//如果没有初始化,初始化
mymalloc_init();
Block *current_location =(Block*)start;//指向内存首地址
void *memory_location = 0; //用于返回开辟空间首地址的指针
while(1)
{
if(current_location == NULL) {
//printf("空间不足\n");
return memory_location;//没有可开辟的空间
}
if(current_location->size >= size + sizeof(Block)
&& current_location->tag == 0 ) break;
current_location = current_location->next;//指向下一个空间
}
//若能执行到这步,说明找到了符合要求的第一个空间
//memory_location指向实际要使用的内存首地址
memory_location = (void*)(current_location) + sizeof(Block);
//创建memory_node为剩余内存标示信息,用于检索
Block *memory_node = (Block*)(memory_location + size);
//双链表指针移动
memory_node->pre = current_location;
memory_node->next = current_location->next;
memory_node->size =current_location->size - size - sizeof(Block);
current_location->next = memory_node;
//把已经使用的空间标志为1,并标示使用了多少内存
current_location->tag = 1;
memory_node->tag = 0;
current_location->size = size;
//返回实际使用内存首地址
return memory_location;
}
/*-------------------malloc最优法---------------------------*/
void* mymalloc_best(size_t size) {
if(!has_initialized)
mymalloc_init();
size_t tmp = N;//tmp初始化为最大空间值
int found = 0;//0表示没有一个空间符合要求
Block *p = (Block*)start;//p初始化,之后指向一个最优的空间
Block *current_location =(Block*)start;
void *memory_location = 0; //用于返回开辟空间首地址的指针
while(current_location != NULL) {
while(1) {
if(current_location == NULL) {
if(found == 0)
return memory_location;//没有可开辟的空间
break;
}
if(current_location->size >= size + sizeof(Block)
&& current_location->tag == 0 ) break;
current_location = current_location->next;
}
if(current_location == NULL) break;//此时found = 1,找寻空间也结束了
if(current_location->size < tmp){
tmp = current_location->size;//保存找到的空间之中最小空间大小值
p = current_location;//指向最小容量的可用空间
found = 1;//表示存在符合要求的空间
}
current_location = current_location->next;
}
current_location = p;//选定最小空间
//切割,原理同最先法后半部分
memory_location = (void*)(current_location) + sizeof(Block);
Block *memory_node = (Block*)(memory_location + size);
memory_node->pre = current_location;
memory_node->next = current_location->next;
memory_node->size =current_location->size - size - sizeof(Block);
current_location->next = memory_node;
current_location->tag = 1;
memory_node->tag = 0;
current_location->size = size;
return memory_location;
}
void myfree(void* ptr){
//获得链表的地址
Block *current_location = (Block*)(ptr - sizeof(Block));
if(current_location->tag != 1) {
perror("not existed!\n");
return;
}
current_location->tag = 0;//把此空间标为空闲
//若所释放空间的下一个空间不为空并且是空闲的
if(NULL != current_location->next
&& 0 == current_location->next->tag)
{
current_location->size +=
current_location->next->size + sizeof(Block);//空间容量相加
current_location->next =
current_location->next->next;//合并完成
//若合并后下一个空间不为空,改变那个空间的前指针
if(NULL != current_location->next)
current_location->next->pre = current_location;
}
//若所释放空间的上一个空间不为空并且是空闲的,原理同上
if(NULL != current_location->pre
&& 0 == current_location->pre->tag)
{
current_location->pre->size +=
current_location->size + sizeof(Block);
current_location->pre->next =
current_location->next;
if(NULL != current_location->next)
current_location->next->pre =
current_location->pre;
}
printf("freed\n");
}
int main(void)
{
char* testarray[ARR_LENGTH]={};
int i=0,index=0;
for(i=0;i<100000;i++)
{
index=random()%ARR_LENGTH;
if (testarray[index]==NULL)
{
testarray[index] = (char*)mymalloc_first(20+random()%100);
//testarray[index] = (char*)mymalloc_best(20+random()%100);
if(testarray[index]) {
strcpy(testarray[index],"this is a test");
puts(testarray[index]);
}
}else
{
myfree(testarray[index]);
testarray[index]=NULL;
}
}
} malloc free
[解决办法]
//memory_location = (void*)(current_location) + sizeof(Block);//编译会出错
memory_location = current_location + 1;
if (current_location->next != NULL)
current_location->next->prev = memory_node;