vc 6.0 's bug?????

F

fisker0303

In vc 6.0:

#include <iostream>
using namespace std;

int main()
{
int a = 10;
int b = 20;
a = (a + b) - (b = a);
cout << "a=" << a << ",b=" << b << endl;
return 0;
}

Release output : a=20,b=10
Debug output: a=10,b=10

why?
 
V

Vladimir Oka

(e-mail address removed) opined:
In vc 6.0:

#include <iostream>
using namespace std;

Got the wrong address. This is comp.lang.c.

Followups-to comp.lang.c++ set...
 
C

CBFalconer

In vc 6.0:

#include <iostream>
using namespace std;

int main()
{
int a = 10;
int b = 20;
a = (a + b) - (b = a);
cout << "a=" << a << ",b=" << b << endl;
return 0;
}

Release output : a=20,b=10
Debug output: a=10,b=10

why?

Because, on close examination of the newsgroups name, you might
detect the absence of the '++' portion. To an eagle eyed
programmer of your mental capability this might carry connotations
indicating that C++ is not the subject under discussion. Such an
alert usenet user might then decide to use comp.lang.c++, and would
still be wrong. VC of any vintage is a proprietary compiler, and
probably bad (considering the origin), but is not on topic in any
newsgroup that discusses a pure language as defined by standards.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
K

Kenneth Brody

In vc 6.0:

#include <iostream>
using namespace std;

int main()
{
int a = 10;
int b = 20;
a = (a + b) - (b = a);
cout << "a=" << a << ",b=" << b << endl;
return 0;
}

Release output : a=20,b=10
Debug output: a=10,b=10

why?

As has been pointed out, this is comp.lang.c, not comp.lang.c++.

However, noting that this is a C answer, and may be 100% wrong for
a C++ program....

a = (a + b) - (b = a);

I believe that this invokes "undefined behavior", because you both
use and modify "b" between sequence points. There is no guarantee
that the "b" in "a+b" will be evaluated before the "b" in "b=a".
You may get the equivalent of:

a = (10 + 20) - (10);
or
a = (10 + 10) - (10);
or
system("format c:");

The fact that you get what appears to be the first behavior for
"release" and the second for "debug" is perfectly acceptable, as
"undefined behavior" means _anything_ can happen.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 

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,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top