【发布时间】:2015-09-23 20:22:11
【问题描述】:
我有这个 alloc 的实现,它将内存分配为一个动态数组。
我的问题是,数组和指针被声明为静态是什么意思?对调用 alloc 的函数有何影响?
#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;
}
【问题讨论】:
-
“我有这个 alloc 的实现,它将内存分配为一个动态数组”。您的代码中似乎没有任何动态数组。
-
@AlanAu 定义 动态
-
您不应该使用
0作为空指针常量。而是使用NULL宏。请注意,即使是 C++ 也吸取了这一教训,支持nullptr。
标签: c memory static allocation