MSVC++ 6.0 Complier optimization bug??

J

jryden

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.
 
J

jryden

Of course I meant COMPILER....

complier postings should go into alt.tools...anyway, the posting is
serious ;)
 
N

Noah Roberts

jryden said:
Of course I meant COMPILER....

complier postings should go into alt.tools...anyway, the posting is
serious ;)

I can't recreate the issue in 7.1
 
P

pillbug

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
{
}
}
 
V

Victor Bazarov

jryden said:
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?

The optimizer of VC6 is buggy. I've seen similare unpleasant things.
#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.

Try declaing 'temp' or 'shift' (or both) "volatile". And keep the
optimization. See what happens.

V
 
P

peter koch

jryden skrev:
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.
[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
 
J

jryden

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

Thanks.
 
J

jryden

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...
 
V

Victor Bazarov

jryden said:
[...] Anyone know where you tell what SP is installed in
6.0. The help|about doesn't show any version numbers...

Please ask product-specific questions in the newsgroup dedicated
to your product. Example: microsoft.public.vc.ide_general.

V
 
J

jryden

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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top