If a variable is declared within a loop, does the previous declarations become garbage? For example, in the following:
loop{
int array[10];
array[i]=......
}
array is declared for each loop iteration. When it is newly declared, is the new memory location that array allocates same with the older location?. If it is not, does the older declarations become garbage, because the allocated area is not freed? Finally, how can it be freed without exiting the loop if the array is static like the above example?
This is an automatic variable that the compiler handles – automatically.
You only have to take care of storage you allocate yourself using
newormalloc. The rest is handled for you.The
arraycomes into scope each time you enter the loop and is destroyed again at the end of each loop. The compiler is very likely to reuse the same space each time, but that is not defined by the language. There will be no garbage either way.