tags:

views:

114

answers:

4

Is there anything speaking against a structure like to following. In Release mode, Visual Studio seems to ignore i < 10 and execute the loop without end. If I set a break point I see that i runs from 0 to 9 and stays at 9 in all further iterations. If I printf i, I get a error after the 10 iteration, because arr only has 10 fields. Very strange. Please help.

for (i = 0; i < 10; i++){
  switch (arr[i].type1){
     case A:
        //something  
        break;

     case B:
        switch (arr[i].type2){
         //something
        }
        break;

     default:
        break;
  }
}

Thanks!


EDIT: I removed all switch statements and replaced them with if statements. Work perfectly now. I still have difficulty believing that I was right and Visual Studio wrong. What about all my other Switch statements in the program? :/

A: 

Change the loop variable to j and make the first line of the loop:

int i = j;

Does it still do it?

dan_waterworth
yes it does ....
Frank
ok, change that to volatile int i = j; and try again
dan_waterworth
if that changes the behaviour it's likely you have a stray ptr.
dan_waterworth
+2  A: 

I suspect that

 // something

might have something to do with it. If you run this code as is (with the actual case code removed) -- does it happen?

How about if you take the entire switch and put it in a function?

You might be changing i somewhere in that // something either directly, or by some memory bounds issue.

Lou Franco
Do a search for i and see if your modifying/assigning it anything else within the for loop. Be specially aware of comparing values in if then statements. The following if statement is an assignment not a compare.It's very easy to forget "==" in "if( i=j ) break".
Dennis Miller
Witch is why I never name variables `i`. Have you tried searching your code for `i` it stops everywhere.
Martin York
A: 

nothing wrong with your example. Perhaps you have some accidental stack-overwriting or something like that in other parts of your code that introduce weird side effects in that place (and no sense making debugging sessions)?

maxschlepzig
A: 

As long as I don't know what // something does, I'll just give you the best idea I have given what I know;

If this only occurs in release mode, I would guess that the optimization done by the compiler may be the cause if this odd behaviour.

If you can make this occur reliably with the exact code given above, I'd say you should try turning off optimization and see if that helps.

Kvisle
how do I do that
Frank
I don't use visual studio myself, but this looks like a good pointer: http://msdn.microsoft.com/en-us/library/aa289168(VS.71).aspx
Kvisle
Release vs debug mode turns up more undefined behavior bugs than it does compiler bugs. More likely there is a pointer error.
Potatoswatter