Why don't static arrays need to be freed?
If the static array is automatically freed after completing the execution, then how can we still access the values of the static array correctly?
Nope, it's not like that. static
variables are initialized before starting main()
and its lifetime is the entire execution of the program. So, they can be return
ed from functions (in which they are defined) and still can be accessed. They are not local (to the functions) which goes out of lifetime when the function finishes execution.
Related, quoting from C11
, chapter §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 specifierstatic
, 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.
Regarding the scope of a static
variable inside a function, yes, it is limited to the function itself, as mentioned in chapter §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. [...]
That means, obviously, you cannot use array a
outside subFunc()
, as a
is not visible outside subFunc()
.
However, when you return
the array (returning an array causes a decay to the pointer to the first element of the array, FWIW), as the lifetime of the static
array is the entire execution of the program, accessing the returned pointer (surely, within bounds) is perfectly valid and legal.
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
Static array or variables will not be freed, when control comes out of that function.
Scope of static variable is local to the function in which it is declared, but its lifetime is throughout the program.
And for a static array in a sub function, the static array will be automatically freed when the called sub function is done.
That is not true. Static arrays are not created when you enter the function, and they are not destroyed when you leave it.
A static variable, and the data inside it, is really a lot like a global variable! The only thing that's local to the function is the name. (You'll hear people talk about the "scope" of the variable -- this means "where can I use the name to refer to it.")
So when you are thinking about the life of a static array, you can mentally replace:
int *subFunc(){
static int a[5] = {1,2,3,4,5};
return a;
}
with
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;
}
and then pretend nobody else in your program can touch that global variable.