When an object is declared and initialized, there are two things that we need to think about:
- Allocation of memory for the object.
- Initializing the object.
When a stack frame is created for a function, the stack frame usually has enough space for not only the automatic variables in the function scope but also local variables that are in the function. In that case, when the line
int b=2;
is executed in the loop, the only thing that happens is the value of the variable is re-initialized in every iteration of the loop.
It's quite possible for a run time environment to push the memory required for b
onto the stack frame when the loop is entered and pop the memory when the loop ends.
If a run time environment uses the first strategy, the address of b
is going to be same in every iteration of the loop.
In a run time environment uses the second strategy, the address of b
will most likely be same in every iteration of the loop. I am hesitant to say a definite "is going to be the same" because I am not sure.