alloc 和 afree来管理allocbuf存储区域

版权声明:原创请勿随意转载。 https://blog.csdn.net/yjysunshine/article/details/81707133

《C程序设计语言》P100 地址运算符

#include <stdio.h>
#define ALLOCSIZE 1000
/*alloc 和 afree来管理allocbuf存储区域*/
char allocbuf[ALLOCSIZE];
char *alloccp = allocbuf;

int alloc(int n);//返回指向n个字符的指针
void afree(char *p);//释放p指向的存储区(p指向存储区的开始部)

//返回指向n个字符的指针【注意函数是char *类型的】
char * alloc(int n)
{
    if(allocbuf + ALLOCSIZE - alloccp >= n){
        alloccp += n;
        return alloccp - n;
    }
    else
        return 0;
}
//释放p指向的存储区
void afree(char *p)
{
    if(p >= allocbuf && p <= allocbuf + ALLOCSIZE)
        alloccp = p;
}
 

猜你喜欢

转载自blog.csdn.net/yjysunshine/article/details/81707133