CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2006
    Posts
    13

    Problem: Release mode versus Debug mode and using COUT resolves it

    I have the following function

    void Schedule::fillUpHomeAwayArray(fixture *s_, int idx, int teamsInDiv, int *homeTeamList, int *awayTeamList) {

    int curDiv = s_[idx].div_;
    int curTeam = s_[idx].homeID_;
    int listIdx = 0;
    int *curList = homeTeamList;
    curList[listIdx] = curTeam;
    listIdx++;

    while(s_[idx].div_ == curDiv) {
    // ** cout << idx << endl;
    if(curTeam != s_[idx].homeID_) {
    curTeam = s_[idx].homeID_;
    curList[listIdx] = curTeam;
    listIdx++;
    }

    if(listIdx == teamsInDiv / 2) {
    curList = awayTeamList;
    listIdx = 0;
    }
    idx++;

    } // while


    }


    It works fine (as expected) in Debug mode (I am using VC++ vers 6).

    But when I run it in release mode, I don't get the same results. It looks as if idx is taking on strange values, and so the values returned are uninitialised.

    BUT, if I uncomment the cout (see line // **), it works in release mode, the same as in debug mode.

    Two questions.

    1. Why does it work in debug mode and not release.
    2. Why does adding the cout make it work in release mode.

    Thanks

  2. #2
    Join Date
    Apr 2008
    Posts
    725

    Re: Problem: Release mode versus Debug mode and using COUT resolves it

    can you give smallest, complete compilable code that will reproduce the code please?

    please explain how the results are different.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Problem: Release mode versus Debug mode and using COUT resolves it

    Quote Originally Posted by gxkendall View Post
    But when I run it in release mode, I don't get the same results. It looks as if idx is taking on strange values, and so the values returned are uninitialised.

    BUT, if I uncomment the cout (see line // **), it works in release mode, the same as in debug mode.
    It doesn't "work" in any mode, release, debug, or in between.

    The program has a bug. It just so happens that the bug is exposed when you run the program a certain way or change it by adding (or removing) superfluous lines of code.
    Two questions.

    1. Why does it work in debug mode and not release.
    Anything you do with pointers or arrays that result in undefined behavior results in programs running erratically. No one knows what those values are that you're sending to the function, whether the values are initialized, how those functions are called, etc.
    2. Why does adding the cout make it work in release mode.
    See first paragraph. The program never works. All you're doing is moving the bug to another part of your program when you add or remove code.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured