静态数组分配
Static array allocation
我有这个 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;
}
My question is that what does it mean that the array and pointer are declared static?
这意味着数组的生命周期是程序的整个执行过程。在文件范围内定义的任何对象(有或没有 static
说明符)都具有静态存储持续时间(例外:使用 C11 _Thread_local
说明符定义的对象)。添加 static
说明符限制对象对它们定义的源文件的可见性。
您的 alloc
分配的总大小受限于您的 allocbuf
数组的大小。
我有这个 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;
}
My question is that what does it mean that the array and pointer are declared static?
这意味着数组的生命周期是程序的整个执行过程。在文件范围内定义的任何对象(有或没有 static
说明符)都具有静态存储持续时间(例外:使用 C11 _Thread_local
说明符定义的对象)。添加 static
说明符限制对象对它们定义的源文件的可见性。
您的 alloc
分配的总大小受限于您的 allocbuf
数组的大小。