MSVC++ 6.0 Complier optimization bug??

Discussion in 'C++' started by jryden, Apr 27, 2006.

  1. jryden

    jryden Guest

    I wrote the following code into a console program in Visual C++6. If
    you build a release build and select custom optimizations and select
    'general optimizations' as the only optimization then you will end up
    with incorrect results. Every line seems to be important, even the
    unrelated bits. Any one have the time or inclination to try this out
    for themselves and try to explain it?

    #include "stdafx.h"
    #include <string>

    int main(int argc, char* argv[])
    {
    std::string s1; <- This is important

    for(int i = 0; i<4; i++)
    {
    int shift = i*4; <- This is important
    short temp = shift; <- This is important
    printf("Generated %d\n", temp);
    }

    return 0;


    while(1) <- This is important, take it out and the problem
    disappears
    {
    }
    }

    The result of this program run with the specified optimizations is as
    follows:
    Generated 0
    Generated 0
    Generated 0
    Generated 0

    Note: This is (very) stripped down version of some production code I
    had written that failed due to this odd bug. For now I plan to just
    remove the optimizations until a proper fix can be determined. I hope
    someone has some insight.
     
    jryden, Apr 27, 2006
    #1
    1. Advertisements

  2. jryden

    jryden Guest

    Of course I meant COMPILER....

    complier postings should go into alt.tools...anyway, the posting is
    serious ;)
     
    jryden, Apr 27, 2006
    #2
    1. Advertisements

  3. jryden

    Noah Roberts Guest

    I can't recreate the issue in 7.1
     
    Noah Roberts, Apr 27, 2006
    #3
  4. jryden

    pillbug Guest

    I compiled the following code using vc6sp5 and could not reproduce
    the error.

    cl -GX -Og test.cpp

    #include <cstdio>
    #include <string>

    int main(int argc, char* argv[])
    {
    std::string s1; // <- This is important

    for(int i = 0; i<4; i++)
    {
    int shift = i*4; // <- This is important
    short temp = shift; // <- This is important
    printf("Generated %d\n", temp);
    }

    return 0;

    while(1) // <- This is important, take it out and the problem
    disappears
    {
    }
    }
     
    pillbug, Apr 27, 2006
    #4
  5. The optimizer of VC6 is buggy. I've seen similare unpleasant things.
    Try declaing 'temp' or 'shift' (or both) "volatile". And keep the
    optimization. See what happens.

    V
     
    Victor Bazarov, Apr 27, 2006
    #5
  6. jryden

    peter koch Guest

    jryden skrev:
    [snip]
    This is off topic but anyway.
    Do not optimise to heavily with VC++ 6.0. So far as I remember /Os or
    /Ot was okay, but more elaborate optimisations went bad.

    /Peter
     
    peter koch, Apr 27, 2006
    #6
  7. jryden

    jryden Guest

    No I tried that myself...it is specific to VC6
     
    jryden, Apr 28, 2006
    #7
  8. jryden

    jryden Guest

    Ah...I'm not sure that I'm running the latest service pack...I make
    sure and then retest the code.

    Thanks.
     
    jryden, Apr 28, 2006
    #8
  9. jryden

    jryden Guest

    Hum...doesn't change anything. Good to cover all the bases though. I
    think I must have actually been running SP because the installation ran
    too quick. Anyone know where you tell what SP is installed in 6.0.
    The help|about doesn't show any version numbers...
     
    jryden, Apr 28, 2006
    #9
  10. Please ask product-specific questions in the newsgroup dedicated
    to your product. Example: microsoft.public.vc.ide_general.

    V
     
    Victor Bazarov, Apr 28, 2006
    #10
  11. jryden

    jryden Guest

    Um...okay...sorry, it just followed the thread of suggestions. Please
    disregard the question.

    In that vein I tried your suggestion to use 'volatile'. This solves
    the problem despite that fact that these are not actually volatile
    variables. This is a better solution than my current solution which is
    to simply turn off the optimizations.

    Since I have a viable solution please consider this thread closed and
    thanks for the help.

    James
     
    jryden, Apr 28, 2006
    #11
  12. jryden

    Default User Guest

    Tried what? See below.



    Brian
     
    Default User, Apr 28, 2006
    #12
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.