The static
keyword, as far as I know, does two things:
- It allocates the variable on the heap rather than the stack. Storage
- It marks the lifetime of the variable as long as the lifetime of its parent process. Scope
So, it is used if:
- The variable is so large that it would overflow the stack. Storage use
- The variable needs to be available for the lifetime of the process for the function. Scope use
But, what if the variable is so large, but doesn't need to be available all the time, and keeping it all the time in the heap would be memory expensive ?
What shall I do if I face that situation ? I'm not sure I totally understand the purpose of the static keyword.