locked
Different compile in DEBUG and RELEASE mode? RRS feed

  • Question

  • I have question for compiler in Microsoft VC++ 2005. I write this code.

     

    Code Block

    ASBool lFound = true;

    while (lFound == true) {

          for (ASTCount i = 0, lFound = false; i < lLen && lFound == false; i++) {...

     

     

     When i run program i debug mode, all is ok. lFound is initialized with false in every begin of for cycle. But if i run program i release mode, every time if program go to for cycle, a new instance of lFound is created. When program goes outside for cycle, new instance of lFound is deleted, and old instanceof lFound (declared outside while cycle) newer change and program freezes.

     

    Why VC++ 2005 compiler compile code differently in DEBUG and RELEASE mode? Setting for release mode is default for Maximize speed. And what bahavior is ok?

     

    Thanks all for commentares.

     

    - Laco.

     

    Tuesday, December 18, 2007 6:10 AM

Answers

  • Does the problem occur if you change the loop?

    Code Block

    ASBool lFound = true;

    while (lFound == true)

    {

        lFound = false;

        for (ASTCount i = 0; . . .

        . . .

     

     

     

    I think the expression "for (ASTCount i = 0, lFound = false; . . ." means that two new variables are created, both of type ASTCount, which are different from any previous i and lFound.

    But when you watch the lFound variable with the Debugger after the loop, probably the results can be affected by a problem reported at http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=140350. I think you should verify the lFound value using some display function instead.

    I hope this helps.

     

    Tuesday, December 18, 2007 7:08 AM

All replies

  • Does the problem occur if you change the loop?

    Code Block

    ASBool lFound = true;

    while (lFound == true)

    {

        lFound = false;

        for (ASTCount i = 0; . . .

        . . .

     

     

     

    I think the expression "for (ASTCount i = 0, lFound = false; . . ." means that two new variables are created, both of type ASTCount, which are different from any previous i and lFound.

    But when you watch the lFound variable with the Debugger after the loop, probably the results can be affected by a problem reported at http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=140350. I think you should verify the lFound value using some display function instead.

    I hope this helps.

     

    Tuesday, December 18, 2007 7:08 AM
  • No problem, i remake the code like this. But is interessant why compiler compiles code differently in DEBUG and RELEASE mode.

    Tuesday, December 18, 2007 7:14 AM
  • I think the ambiguous behaviour can be caused by this difference between Debug and Release modes: the debug version of the runtime library initializes dynamic memory and stack variables to known values, while the release version doesn't [http://www.codeguru.com/forum/showthread.php?t=269905]. Maybe you have some other variables or pointers which are not handled correctly and which cause, for example, overwriting of the first lFound variable? During investigations, you should take into consideration that you actually have two different lFound variables in your original code.

     

    Tuesday, December 18, 2007 7:54 AM