看书写了一段代码用来分配内存。以下是头文件。
#ifndef MEMPOOL_H_INCLUDED
#define MEMPOOL_H_INCLUDED
#include 
#include 
#define max_list 10
struct p_data //基本的池单元
{
 struct p_data *pre;
 void *avi;
 void *limi;
 unsigned long p_size;
};
struct p_data *head=NULL;
struct p_data *start=NULL;
void new_pool(void) //开辟新的内存池
{
 if(head!=NULL||start!=NULL)
 {
 printf("You has created a pool\n");
 return;
 }
 head=(struct p_data *)malloc(sizeof(*head));
 start=(struct p_free *)malloc(sizeof(*start));
 if(start==NULL||head==NULL)
 {
 printf("The environment doesn't allow the allocation of memory\n");
 return;
 }
 head->pre=NULL;
 head->avi=NULL;
 head->limi=NULL;
 head->p_size=0;
 start->limi=NULL;
 start->avi=NULL;
 start->pre=NULL;
 start->p_size=0;
}
void *p_malloc(unsigned long n_size) //在池中开辟新的内存单元
{
 void *re_p=NULL;
 struct p_data *now=NULL;
 struct p_data *go=NULL;
 if(!(n_size>0))
 {
 printf("The size must be bigger than zero\n");
 return NULL;
 }
 if(head->pre!=NULL)
 {
 now=head;
 for(;;now=now->pre)
 {
 if((now->limi-now->avi)>=n_size)
 {
 re_p=now->avi;
 now->avi=(char *)now->avi+n_size;
 return re_p;
 }
 if(now->pre->pre==NULL)break;
 else continue;
 }
 }
 if(start->pre!=NULL)
 {
 now=start->pre;
 go=start;
 for(;;now=now->pre)
 {
 if(now->p_size>n_size)
 {
 if(now->pre==NULL)go->pre=NULL;
 else go->pre=now->pre;
 for(go=head;;go=go->pre)
 if(go->pre==NULL)break;
 go->pre=now;
 now->pre=NULL;
 re_p=(char *)now+sizeof(struct p_data);
 go->avi=(char *)re_p+sizeof(struct p_data)+n_size;
 go->limi=(char *)re_p+sizeof(struct p_data)+now->p_size;
 memset((char *)now+sizeof(struct p_data),'\0',now->p_size);
 start->p_size--;
 head->p_size++;
 return re_p;
 }
 if(now->pre==NULL)break;
 else go=now;
 }
 }
 if((now=(struct p_data *)malloc(sizeof(struct p_data)+n_size+1024*10))==NULL)
 {
 printf("The environment doesn't allow the allocation of memory.\n");
 return NULL;
 }
 now->pre=NULL;
 now->p_size=n_size+10*1024;
 now->limi=NULL;
 now->avi=NULL;
 for(go=head;;go=go->pre)
 {
 if(go->pre==NULL)break;
 }
 go->pre=now;
 re_p=(char *)now+sizeof(struct p_data);
 go->avi=(char *)now+sizeof(struct p_data)+n_size;
 go->limi=(char *)now+sizeof(struct p_data)+n_size+1024*10;
 head->p_size++;
 return re_p;
}
void clean_pool(void) //将所有池内存转移到空白列
{
 if(head==NULL||head->pre==NULL)
 {
 printf("There's no data in the pool\n");
 return;
 }
 struct p_data *now=head->pre;
 struct p_data *mid=NULL;
 for(;;)
 {
 now->avi=NULL;
 now->limi=NULL;
 mid=now->pre;
 now->pre=start->pre;
 start->pre=now;
 start->p_size++;
 if(mid==NULL||start->p_size==max_list)break;
 else now=mid;
 }
 if(now->pre!=NULL&&max_list==start->p_size)
 {
 now=now->pre;
 mid=now;
 now=now->pre;
 for(;;)
 {
 free(&mid);/*循环若干次后就是这里出了问题。。*/
 if(now->pre==NULL)
 {
 mid=now;
 free(&mid);
 break;
 }
 mid=now;
 now=now->pre;
 }
 }
 head->pre=NULL;
 head->limi=NULL;
 head->p_size=0;
 head->avi=NULL;
}
#endif // MEMPOOL_H_INCLUDED
以下是驱动小程序
#include 
#include "mempool.h"
int main(void)
{
 int n;
 int m=0;
 char *test=NULL;
for(;;)
{
 new_pool();
 for(n=0;n<15;n++)p_malloc(10240+1);
 printf("%d",m++);
 clean_pool();
}
return 0;
不知道为什么在循环若干次后会出现c0000005问题... 
经排查是在free()出的问题。是内存泄露吗?
回复讨论(解决方案)
一般free出错很可能是你动态分配的内存访问时越界造成的