2
void main(){
   int a=1;

   for(int i=0;i<10;i++){
      int b=2;
   }
}

In the loop, variable B will be created every time in the loop,right? If so, I check the address of variable b by using & operation and found that address is exactly the same. If it's not recreated, the b should be still in the stack. Every time it will declare a new variable b in the loop. why no redefinition error happened here?

Martingale
  • 83
  • 5

5 Answers5

2

A (non-static) variable defined in a block is re-created every time that block is entered.

If it's a local variable, it may easily reside at the same address on successive entries to the block. In the case of an int, nothing needs to be done (other than allocating space) to create the variable.

On the other hand, if you were to create an object of a type with a constructor that did something, whatever the constructor did would execute every time that block was entered (and the destructor would run every time execution left the block).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • So you can say it is allocated in the same memory space by coincidence every loop? – Ediac Jun 08 '15 at 20:26
  • @CNomad: it's not exactly coincidence. You (usually) have the same start condition and algorithm to get to the next step. It's no real surprise that the next step it takes will *usually* match, unless it has a reason to do things differently. – Jerry Coffin Jun 08 '15 at 20:29
0

The creation of the variable for each iteration is optimized out, and instead it's simply initialized once every iteration through the loop. See this question.

Community
  • 1
  • 1
jaggedSpire
  • 4,423
  • 2
  • 26
  • 52
0

There's a new b in the same location on every iteration.
Since the "old" one doesn't exist on the following iteration, the space it occupied can be reused.
If this didn't happen, you'd run out of memory very quickly.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82
0

When an object is declared and initialized, there are two things that we need to think about:

  1. Allocation of memory for the object.
  2. 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.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

Local variables are not created on the heap. We say that they are created on stack. But in reality, registers are used to store the local variables, if available. Storing in registers is far less expensive than storing in stack. Every-time the line int b = 2 is encountered in the loop, the register is re-written. Therefore, you will not get a redefinition error. That's the reason the value of b is lost every iteration.

ryog
  • 122
  • 4