/O2 bug?

  • Thread starter Serge Skorokhodov (216716244)
  • Start date
S

Serge Skorokhodov (216716244)

Hi,

I don't know whether this is a known buf. The following snippet:

== start ==
#include "stdafx.h"
#include <iostream>
#include <tchar.h>

int _tmain(int argc, _TCHAR* argv[])
{
char c[4];
c[0]='a';
c[1]='b';
c[2]='c';
c[3]='d';

std::cout << "before: " << c[0] << c[1] << c[2] << c[3] << " ";

for (int n=2 ; n>0; --n)
{
for (int i=0; i<3; i++)
{
c = c[i+1];
}
c[3] = 'x';
}

std::cout << "after: " << c[0] << c[1] << c[2] << c[3] <<
std::endl;
return 0;
}
== end ==
produces output "before: abcd after: cdxx" without /O2
and "before: abcd: after: cxxx" with /O2

TIA
 
C

Carl Daniel [VC++ MVP]

Serge Skorokhodov (216716244) said:
== end ==
produces output "before: abcd after: cdxx" without /O2
and "before: abcd: after: cxxx" with /O2

With VC++ 2005 the output is correct with or without /O2.

-cd
 
T

Tom Widmer [VC++ MVP]

Serge said:
Oh, sorry. I'm using VC++ 2003

Confirmed on 2003. The code doesn't look like it has any undefined,
unspecified or implementation defined behaviour.

Tom
 

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

Top