update: I've now re-tested both a simplified test case and the full code with the shared names reinstated, and it works correctly. There probably was some other error somewhere else in my code which is now fixed. Sorry for wasting your time and effort; will never post without a working test case in the future. Mea culpa.
I have a C++ function which I call repeatedly. It has the following snippet in it
switch(c)
{
case 1:
{
static int i = 0;
if ( ... ) { i = 0; }
....
break;
}
case 2:
{
static int i = 0;
if ( ... ) { i = 0; }
....
break;
}
case 3:
{
static int i = 0;
if ( ... ) { i = 0; }
....
break;
}
}
The idea is that it must remember its state per each case, and sometimes it must reset it.
It didn't work properly. When I was debugging it (MSVC++ 2010 Express Edition) I noticed that each i
was not behaving independently and their values were changing seemingly by themselves; moreover when the re-set condition was hit, the corresponding if
was entered OK but the i = 0;
statement was just ... skipped over!!! And the "locals" window showed i
with its previous value, unchanged (while the current statement was the next one, still inside the if
). Other statements inside the if
were executing properly.
When I renamed each i
with a unique name (i1
, i2
, i3
), the problem went away.
Is this some bug, or some language feature that I should know? I thought each block { ... }
defines independent scope. What is going on here? Would it work in C?
edit: sorry for not constructing the test case. Will do so, and report back later.