| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 769 人关注过本帖
标题:大家来帮我看看这个程序错在哪里?
取消只看楼主 加入收藏
longziyong
Rank: 2
来 自:四川省威远县
等 级:禁止访问
帖 子:77
专家分:23
注 册:2007-8-22
收藏
 问题点数:0 回复次数:4 
大家来帮我看看这个程序错在哪里?
#define BLKSIZ 80
#define STKSIZ 1000

typedef union{
   char *link;
   char data[BLKSIZ];
}BLOCK;

static BLOCK *btm_of_heap;
static BLOCK *top_of_heap;
static BLOCK *heap;

void Init_Heap(){
   BLOCK *ptr;
   int blk;
   heap=(BLOCK *)malloc(BLKSIZ);
   btm_of_heap=heap;
   ptr=heap;
   for(blk=0; blk<49; blk++){
      ptr->link=(BLOCK *)malloc(BLKSIZ);
      ptr=ptr->link;
   }
   ptr->link=NULL;
   top_of_heap=ptr;
}

void My_Free(BLOCK *ptr){
   if(ptr>=btm_of_heap){
      if(ptr<top_of_heap){ /*Block is from my heap*/
         ptr->link=heap;
         heap=ptr;
         return;
      } else if(free(ptr)){ /*Block is from C-Ware's heap*/
         return;
      }
   }
   puts("\nAttempt to free unallocated block!\n\7");
   exit(1);
}

BLOCK *Allocate(int bytes){
   BLOCK *ptr;
   if(bytes<=BLKSIZ){ /* 要求的内存块大小小于链表维护的内存块大小 */
      if(heap!=0){ /* 有空闲块 */
         ptr=heap;
         heap=heap->link;
         return ptr;
      }
   }
   if(ptr=(BLOCK *)malloc(bytes)){ /* 要求的内存块大小大于链表维护的内存块大小或者没有空闲块 */
      return ptr;
   }
   puts("Insufficient memory:malloc()\n\7");
   exit(1);
}

void main()
{
   BLOCK *main_ptr
   Init_Heap();
   main_ptr=Allocate(80);
   My_Free(main_ptr);
}
2008-12-09 17:45
longziyong
Rank: 2
来 自:四川省威远县
等 级:禁止访问
帖 子:77
专家分:23
注 册:2007-8-22
收藏
得分:0 
补充:错误提示截图:
图片附件: 游客没有浏览图片的权限,请 登录注册

LZY'Blog http://www.
2008-12-09 17:48
longziyong
Rank: 2
来 自:四川省威远县
等 级:禁止访问
帖 子:77
专家分:23
注 册:2007-8-22
收藏
得分:0 
请高手帮忙看看吧,小弟实在改正不了。

LZY'Blog http://www.
2008-12-09 18:32
longziyong
Rank: 2
来 自:四川省威远县
等 级:禁止访问
帖 子:77
专家分:23
注 册:2007-8-22
收藏
得分:0 
谢谢楼上大哥的帮忙,程序中还有一个警告,再请帮忙看看:
图片附件: 游客没有浏览图片的权限,请 登录注册

LZY'Blog http://www.
2008-12-10 15:41
longziyong
Rank: 2
来 自:四川省威远县
等 级:禁止访问
帖 子:77
专家分:23
注 册:2007-8-22
收藏
得分:0 
没办法,自己搞不清楚这个问题究竟是出在哪里啊,虽然经过5楼的大哥改正后,程序能够通过编译了,但还存在这么一个警告,不知为何?

LZY'Blog http://www.
2008-12-10 17:38
快速回复:大家来帮我看看这个程序错在哪里?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014390 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved