Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why don't static arrays need to be freed?

I am wondering why static arrays don't need to be freed? I know that when creating a dynamic array e.g.

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

we have to free the allocated memory by using:

free(p); 

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

What I do not understand is: when returning a static array using a function like this:

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?

like image 855
BL_ Avatar asked May 10 '16 07:05

BL_


People also ask

What are advantages of static arrays?

Static Arrays: You use them when you know at compile time the size of the array. Size of this will not change during the execution of the program. Memory requirements works just as same for any other variable.

Do you need to delete static array C++?

No, you don't need to delete it because array has automatic storage duration. It will be released when goes out of each while loop. You need to call delete [] / new [] , and delete / new in pairs.


1 Answers

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 returned 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 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.

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.

like image 95
Sourav Ghosh Avatar answered Oct 08 '22 18:10

Sourav Ghosh