CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2012
    Posts
    2

    Angry VC++ numerical difference executable

    Hi All,

    Does anybody know why I get difference numerical results when I run the executable file directly and when I use the VS environment to run the program?

    Thanks a lot,
    Arash

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

    Re: VC++ numerical difference executable

    Quote Originally Posted by ArashCKar View Post
    Hi All,

    Does anybody know why I get difference numerical results when I run the executable file directly and when I use the VS environment to run the program?
    Your program has a bug.

    Let's see the code.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Jul 2012
    Posts
    2

    Re: VC++ numerical difference executable

    Thank you Paul,

    It is a huge code. I cannot post it. It works fine when ran from the debugger both under debug and release mode but when running the executable under exactly identical circumstances produce a different result.

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: VC++ numerical difference executable

    Not much we can do without code. You probably didn't initialize something.

  5. #5
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211

    Re: VC++ numerical difference executable

    Its just like asking a doctor that you are sick and give you a medicine, where you can't tell the doctor that what is wrong with you, neither you let him examine you

    There can be so many things that go wrong, such as un-initialization of something (as pointed out by GCDEF) or if you are using floats/doubles then due to floating point inaccuracy, you may get different numerical results, who knows...

    Try to replicate the issue in a small test program and post it here.

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

    Re: VC++ numerical difference executable

    Quote Originally Posted by ArashCKar View Post
    It works fine when ran from the debugger both under debug and release mode but when running the executable under exactly identical circumstances produce a different result.
    You are not running the program "under exactly identical circumstances". One way, you're running under the debugger, and in another way, you're not running under a debugger. Those are two different circumstances. If your C++ program has certain types of bugs, then there is no guarantee how it will run or what results you will get. It could even run OK today and not run OK tomorrow.

    As others pointed out,

    1) You may have uninitialized variables -- this means that your program will exhibit undefined behaviour.
    2) You may not be checking all return values from functions you're calling, causing your program to exhibit undesired behaviour.
    3) You may be expecting to read values from a file, and the file doesn't exist or it's the wrong file (again, causing different behaviour).
    4) You may be overrunning the boundaries of an array or trashing memory in some other way, meaning your program will exhibit undefined behaviour.

    I can list many more things.
    It is a huge code. I cannot post it.
    So what do you want us to do? We can't solve problems without seeing what you're doing.

    All I can say is that your program has a bug, and you need to debug it. Having a program behave differently when run under a debugger, on another computer, on a different day of the week, etc. is not uncommon, and it's all due to the items I listed above (and even more reasons I didn't list).

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; July 5th, 2012 at 08:31 PM.

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

    Re: VC++ numerical difference executable

    Quote Originally Posted by ArashCKar View Post
    Thank you Paul,

    It is a huge code. I cannot post it. It works fine when ran from the debugger both under debug and release mode but when running the executable under exactly identical circumstances produce a different result.
    Code:
    #include <iostream>
    class foo
    {
       bool bValue;
       public:
            bool getValue() const { return bValue; }
    };
    
    int main()
    {
        foo f;
        double dValue;
        if ( f.getValue() )
            dValue = 1.0;
        else
            dValue = 0.0;
        std::cout << dValue;
    }
    Run this program. What do you expect to be printed out on the console?

    This is a perfect, tiny example of a program that can exhibit different behaviour, all because of an uninitialized variable.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; July 5th, 2012 at 12:58 AM.

  8. #8
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: VC++ numerical difference executable

    If this code uses floating point (float, double), then results can be different in debug and release builds due to optimisations.

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