2
#define ALLOCSIZE 10000 /* size of available space */
static char allocbuf[ALLOCSIZE]; /* storage for alloc */
static char *allocp = allocbuf; /* next free position */
char *alloc(int n) /* return pointer to n characters */
{
if (allocbuf + ALLOCSIZE - allocp >= n) { /* it fits */
allocp += n;
return allocp - n; /* old p */
} else /* not enough room */
return 0;
}
void afree(char *p) /* free storage pointed to by p */
{
if (p >= allocbuf && p < allocbuf + ALLOCSIZE)
allocp = p;
}
到目前为止,我所理解的是内存分配的目的是为了保持程序的高效性。不会声明一个数组allocbuf占用了所有这些空间并破坏了目的?我真诚地感谢任何回答的人。用c中的数组进行LIFO内存分配的目的是什么?
你的代码没有任何LIFO内存分配,它只是从头开始分配它。 – bluehallu
这里唯一的“目的”是保持代码简单。这是非常危险的代码。 –