为什么不需要释放静态数组?

Why don't static arrays need to be freed?

我想知道为什么不需要释放静态数组?我知道在创建动态数组时,例如

int *p;
p = malloc(10*sizeof(int));

我们必须使用以下方法释放分配的内存:

free(p);

并且对于函数中的静态数组,当调用的函数完成时,静态数组会自动释放。

我不明白的是:当使用这样的函数返回静态数组时:

int *subFunc(){
    static int a[5] = {1,2,3,4,5};
    return a;
}

int main(){
    int *p;
    p = subFunc();
}

如果静态数组在执行完成后自动释放,那我们如何才能正确访问静态数组的值呢?

If the static array is automatically freed after completing the execution, then how can we still access the values of the static array correctly?

不,不是这样的。 static 变量在启动前被初始化 main() 并且它的生命周期是整个程序的执行。因此,它们可以从函数(定义它们的地方)returned 并且仍然可以访问。它们不是 local(对于函数),当函数完成执行时,它们会超出生命周期。

相关,引用自 C11,章节 §6.2.4

An object whose identifier is declared without the storage-class specifier _Thread_local, and either with external or internal linkage or with the storage-class specifier static, has static storage duration. Its lifetime is the entire execution of the program and its stored value is initialized only once, prior to program startup.

关于函数内部static变量的范围,是的,它仅限于函数本身,如§6.2.1章节所述,

[...] If the declarator or type specifier that declares the identifier appears inside a block or within the list of parameter declarations in a function definition, the identifier has block scope, which terminates at the end of the associated block. [...]

这意味着,很明显,你不能在 subFunc() 之外使用数组 a,因为 asubFunc() 之外 不可见 ].

但是,当您 return 数组时(返回数组会导致指向数组第一个元素的指针衰减,FWIW),因为 static 数组的生命周期是在程序的整个执行过程中,访问返回的指针(当然,在范围内)是完全有效和合法的。

Static variables continue to exist even after the block in which they are defined terminates. Thus, the value of a static variable in a function is retained between repeated function calls to the same function. The scope of static automatic variables is identical to that of automatic variables, i.e. it is local to the block in which it is defined; however, the storage allocated becomes permanent for the duration of the program. Static variables may be initialized in their declarations; however, the initializers must be constant expressions, and initialization is done only once at compile time when memory is allocated for the static variable. - source

当控制从该函数中退出时,静态数组或变量将不会被释放。

Scope static 变量在声明它的函数中是局部的,但它的 lifetime 是贯穿整个程序的。

And for a static array in a sub function, the static array will be automatically freed when the called sub function is done.

事实并非如此。进入函数时不创建静态数组,离开函数时不销毁静态数组。

一个静态变量,以及里面的数据,真的很像一个全局变量!该函数唯一的局部是 name。 (你会听到人们谈论变量的 "scope" —— 这意味着 "where can I use the name to refer to it.")

所以当你在思考静态数组的生命周期时,你可以在心里换成:

int *subFunc(){
    static int a[5] = {1,2,3,4,5};
    return a;
}

int ONLY_USE_ME_INSIDE_SUBFUNC__a[5] = {1,2,3,4,5};  /* global variable */

int *subFunc(){
    int * a = ONLY_USE_ME_INSIDE_SUBFUNC__a;  /* a is the same as the global */
    return a;
}

然后假装您的程序中没有其他人可以触及该全局变量。

Static variables 在函数内部,通常 用于在多次调用函数后在函数范围内维护一些数据 。它们在 main() 之前初始化,并且它们的生命周期是整个程序的执行。因此,如果它们在退出函数后被释放,那将没有意义。如果释放它们,下次调用函数时就会崩溃,因为它们不会被引用。

I am wondering why static arrays need not to be freed?

  1. 内存管理函数(malloc、calloc)未分配的任何内容,例如 int a[5] 不需要明确处理 freeing.

  2. 静态变量,例如 static int a[5] 用于在本地范围内访问(它们在本地函数的后续调用之间保留其值)。它们正是为此目的在编译时创建的,它们有程序生命周期,因此释放它们不是合乎逻辑的考虑,即使有可能,而不是

  3. 其他答案中已巧妙地解释了其他所有内容。