免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2431 | 回复: 2
打印 上一主题 下一主题

[C] 模拟内存分配,为什么打印不出"堆"内存信息? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-03-27 11:44 |只看该作者 |倒序浏览
5可用积分
迷惑,同样的代码在vc++(vs2005)下可输出“堆”信息,而在gcc下却显示不出?望达人解惑!
代码如下:

//1). mem.h
#ifndef __MEM_H__
#define __MEM_H__
#include <stdio.h>
#include <string.h>
#define _1K 1024
#define _1M 1024*_1K
#define _1G 1024*_1M
#define BUFSIZE _1M
static char buf[BUFSIZE] ;
static unsigned int front = 0;
typedef struct tagMemInfo
{
   int token;
   int start;
   int end;
   int used;
   struct tagMemInfo *next;
}MemInfo;
MemInfo header;
MemInfo* current;
//interface
void* s_malloc(unsigned int size);
void s_free(void* memptr);
//private
void* alloc_header(unsigned int* nfront);
unsigned int gap(MemInfo* memptr1,MemInfo* memptr2);
void* memloc(MemInfo* ptr,unsigned int* pos,unsigned int size);
void* realloc(unsigned int size);
#endif

//2).mem.c
#include "mem.h"
void* alloc_header(unsigned int* nfront)
{
if(*nfront + sizeof(MemInfo) < BUFSIZE)  
{  
  *nfront +=  sizeof(MemInfo) ;      
  return &buf[(int)*nfront - sizeof(MemInfo)];
}
else
{
  return NULL;
}
}
unsigned int gap(MemInfo* memptr1,MemInfo* memptr2)
{
if(memptr1 != NULL && memptr2 != NULL && memptr1->end != 0)
{  
  return memptr2->start - memptr1->end - sizeof(MemInfo) -1 ;
}
else
{
  return 0;
}
}
void* memloc(MemInfo* ptr,unsigned int* pos,unsigned int size)
{
MemInfo* memPtr = (MemInfo*)alloc_header(pos);  
memPtr->start = *pos;
memPtr->end = memPtr->start + size -1;
memPtr->used = 1;     
MemInfo* tmp = ptr->next;
if(tmp != NULL)
{
  ptr->next = memPtr;
  memPtr->next = tmp;  
}
else
{
  ptr->next = memPtr;
  current = memPtr;
}
memPtr->token = (int)&buf[memPtr->start];
*pos += size;
return &buf[memPtr->start];
}
void* realloc(unsigned int size)
{
unsigned int pos;
unsigned int nGap = 0;
MemInfo* ptr = &header;
while(ptr!= NULL)
{
  if((ptr->end - ptr->start >= size) && (ptr->used == 0))  
  {
   ptr->used = 1;
   ptr->end = ptr->start + size -1 ;   
   if( front < ptr->end)
   {
    front = ptr->end + 1;
   }
   return (char*)ptr + sizeof(MemInfo);
  }
  MemInfo* tmp = ptr;
  ptr = ptr->next;
  if(ptr != NULL)
  {
   printf("%d\n %d\n",ptr->start,tmp->end);
   nGap = gap(tmp,ptr);  
   printf("gap is %d\n",nGap);
  }
  if( nGap >= (size + sizeof(MemInfo)))
  {
   pos = tmp->end + 1 ;
   return memloc(tmp,&pos,size);
  }
}  
return NULL;
}

void* s_malloc(unsigned int size)
{
if(current == NULL)
{
  header.next = NULL;
  current = &header;
}
void* ptr = realloc(size);
if(ptr!=NULL)
{
  return ptr;
}
else
{
  if(front + sizeof(MemInfo) < BUFSIZE)
  {           
      return memloc(current,&front,size);
  }
  else
  {
   return NULL;
  }

}

}
void s_free(void* memptr)
{
MemInfo* ptr = &header;
while(ptr != NULL )
{
  if(ptr->token == (int)memptr)
  {
   ptr->used = 0;
   if(ptr->next == NULL)
   {
    front -= (ptr->end - ptr->start) + 1;
   }
   break;
  }
  ptr = ptr->next;
}
}

//3).main.c

#include "mem.h"

int main()
{
int i;
char* ptr;
char* ptr3;
char* ptr4;
char* ptr5;
char* ptr6;

ptr = (char*)s_malloc(100);
        strcpy(ptr,"hello world!");
printf("%s\n",ptr);
ptr3 = (char*)s_malloc(7);
strcpy(ptr3,"sjshwy");

printf("%s\n",ptr3);

s_free(ptr);

ptr4 = (char*)s_malloc(5);
strcpy(ptr4,"win!");
printf("%s\n",ptr4);

ptr5 = (char*)s_malloc(20);
strcpy(ptr5,"i love you!");
printf("%s\n",ptr5);
ptr6 = (char*)s_malloc(100);
strcpy(ptr6,"it is the end of demo!");
printf("%s\n",ptr6);
printf("...... heap info ......\n");
for(i = 0; i< 400; ++i )
{
  
  printf("%c",buf);  
}
printf("\n......End of heap info ......\n");
MemInfo* memptr = &header;
while(memptr != NULL)
{
  printf("%s\n",(char*)memptr + sizeof(MemInfo));
  memptr = memptr->next;
}
printf("\n");

return 0;
}

论坛徽章:
0
2 [报告]
发表于 2008-03-27 15:25 |只看该作者
自己顶一下了

论坛徽章:
0
3 [报告]
发表于 2008-03-31 10:10 |只看该作者
已解决,都是static惹的祸。。。。。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP