what does it mean memory will not be available after function over for static array?
Static array allocation in function
Hello @gauravlodhi21,
Can you please explain what are you trying to ask?
As per my understanding of the statement you have written:
When ever you call a function, it will be assigned a block of memory called stack frame in call stack.
Inside that stack frames, the variables local to that function will be assigned memory too.
The time you will return from the function, the stack frame will be deallocated and the local variables will destroy.
Now, suppose in your code you are calling the same function say student() multiple times.
Each call is made for a separate student.
Your task is to keep track of the number of students i.e. the number of function calls. Correct?
How can we do that?
The answer is a Static variable.
When a variable is declared as static, space for it gets allocated for the lifetime of the program. Even if the function is called multiple times, space for the static variable is allocated only once and the value of variable in the previous call gets carried through the next function call.
In simple words, there will be a single copy of this variable which will be common to all function calls.
Example:
void student(){
static int temp=0; //executed only once
temp=temp+1; // student count
}
Hope, this would help.
Give a like if you are satisfied.