gpt4 book ai didi

c - 静态数组分配

转载 作者:行者123 更新时间:2023-11-30 20:24:43 27 4
gpt4 key购买 nike

我有 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 说明符)都具有静态存储持续时间(异常(exception):使用 C11 _Thread_local 说明符定义的对象)。添加 static 说明符会限制对象对其定义的源文件的可见性。

alloc 分配的总大小受到 allocbuf 数组大小的限制。

关于c - 静态数组分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32748784/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com