Back

#include<stdio.h>

#define   ALLOCSIZE  10000

static char allocbuf[ALLOCSIZE];
static char *allocp = allocbuf;
char *alloc(int n);
void afree(char *p);

int main()
{
int nbyte;
char msg[] = "Four score and seven years ago, our forefathers ...";
printf("%s\n",msg);
nbyte = (int) alloc(sizeof(msg));
printf("original address = %p  Msg address = %p \n",allocbuf, allocp);
printf("Number of bytes allocated = %d \n",allocp-nbyte);

return 0;
}

char *alloc(int n)
{
  if(allocbuf + ALLOCSIZE - allocp >= n)
   {
      allocp += n;
      return allocp -n;
   }
  else
   return 0;
}

void afree(char *p)
{
  if (p >= allocbuf && p < allocbuf + ALLOCSIZE)
     allocp = p;
}

Top