473,898 Members | 2,558 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Horrible Visual C Bug!

Hello,

have you ever heard about this MS-visual c compiler bug?
look at the small prog:
static int x=0;
int bit32() {
return ++x;
}

int bit64() {
return bit32() + (bit32() << 1);
}

void main(int argc, char **argv) {
int i;
for (i = 0; i < 5; i++) printf("%d. %d\n", i, bit64());
}
Ok, the (correct) result is:

0. 2
1. 8
2. 14
3. 20
4. 26

This is what every compiled progam says. Inclusive MSVisualC Compiler
with Debug options or /Ot fast-option.

But do not dare to switch to the the /O2 option of MSVisualC Compiler.
Then once your computer cannot calculate anymore:

0. 1
1. 7
2. 13
3. 19
4. 25

So, up to Microsoft, 0 + 2 = 1 ?????. That's why their OS is so stable....
Try to increase the "<< 1". It even gets worse.

Ever seen this? I costed me hours of debugging. Can I
sue Microsoft for this?

- Oliver Brausch

http://home.arcor.de/dreamlike
Nov 13 '05 #1
140 7944
"Oliver Brausch" <ob***@web.de > wrote in message
news:cd******** *************** **@posting.goog le.com...
have you ever heard about this MS-visual c compiler bug?
look at the small prog:
It is not a compiler bug. It is a bug in your code.
static int x=0;
int bit32() {
return ++x;
}

int bit64() {
return bit32() + (bit32() << 1);
}
You try to change a variable twice, which is invalid, ie. undefined
behaviour. Microsoft's compiler is absolutely right.
void main(int argc, char **argv) {
'void main' is illegal. It is *always* 'int main'.
int i;
for (i = 0; i < 5; i++) printf("%d. %d\n", i, bit64());
} This is what every compiled progam says. Inclusive MSVisualC Compiler
with Debug options or /Ot fast-option.
So? Just because many compiled programs say this should be the result
does not mean that they are right.
Ever seen this? I costed me hours of debugging. Can I
sue Microsoft for this?


No, but your boss could fire you for such C code, since it is invalid.
--
jb

(replace y with x if you want to reply by e-mail)
Nov 13 '05 #2
Oliver Brausch <ob***@web.de > scribbled the following
on comp.lang.c:
Hello, have you ever heard about this MS-visual c compiler bug?
look at the small prog:
static int x=0;
int bit32() {
return ++x;
} int bit64() {
return bit32() + (bit32() << 1);
These two bit32() calls can be evaluated in either order, and the
implementation doesn't even have to be consistent about it.

Assume x==0. The first way:
bit32() + (bit32() << 1) ==
1 + (bit32() << 1) ==
1 + (2 << 1) ==
1 + (4) ==
5
The second way:
bit32() + (1 << 1) ==
bit32() + (2) ==
2 + (2) ==
4
} void main(int argc, char **argv) {
You have induced undefined behaviour by using void main() and lost all
right to expect any kind of specific behaviour at all.
int i;
for (i = 0; i < 5; i++) printf("%d. %d\n", i, bit64());
}
Ok, the (correct) result is:
Anything. Any result is correct, because of the void main().
0. 2
1. 8
2. 14
3. 20
4. 26 This is what every compiled progam says. Inclusive MSVisualC Compiler
with Debug options or /Ot fast-option. But do not dare to switch to the the /O2 option of MSVisualC Compiler.
Then once your computer cannot calculate anymore: 0. 1
1. 7
2. 13
3. 19
4. 25
You'll have to read up on undefined behaviour (void main()) and
unspecified behaviour (bit32() + (bit32() << 1)).
So, up to Microsoft, 0 + 2 = 1 ?????. That's why their OS is so stable....
Try to increase the "<< 1". It even gets worse. Ever seen this? I costed me hours of debugging. Can I
sue Microsoft for this?


No. It's neither their or C's fault. It's yours.

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"There's no business like slow business."
- Tailgunner
Nov 13 '05 #3
Oliver Brausch wrote:
Hello,

have you ever heard about this MS-visual c compiler bug?
Nope.
look at the small prog:
OK...
static int x=0;
int bit32() {
return ++x;
}

int bit64() {
return bit32() + (bit32() << 1);
}

void main(int argc, char **argv) { int main(int argc, char **argv) {

but since you're not making use of the command-line interface,

int main(void)

would do quite nicely...
int i;
for (i = 0; i < 5; i++) printf("%d. %d\n", i, bit64());
}
Ok, the (correct) result is:

0. 2
1. 8
2. 14
3. 20
4. 26
Oh, really?

What if I told you that the correct result could be just about anything,
including demons flying out of your nose?

This is what every compiled progam says. Inclusive MSVisualC Compiler
with Debug options or /Ot fast-option.

But do not dare to switch to the the /O2 option of MSVisualC Compiler.
Then once your computer cannot calculate anymore:

0. 1
1. 7
2. 13
3. 19
4. 25

So, up to Microsoft, 0 + 2 = 1 ?????. That's why their OS is so stable....
Try to increase the "<< 1". It even gets worse.
Well, as it turns out, the bug is on the other side of the keyboard in
this case.[1]

Please see: http://www.eskimo.com/~scs/C-faq/s3.html for an explanation.

Ever seen this? I costed me hours of debugging. Can I
sue Microsoft for this?


Actually, even if it had been their bug (which it isn't) you couldn't --
read the license.

HTH,
--ag

[1] Not that I would mind a bit had it been _their_ fault.

--
Artie Gold -- Austin, Texas

Nov 13 '05 #4
In article <bf************ *@news.t-online.com>,
Jakob Bieling <ne*****@gmy.ne t> wrote:
"Oliver Brausch" <ob***@web.de > wrote in message
news:cd******* *************** ***@posting.goo gle.com...
static int x=0;
int bit32() {
return ++x;
}

int bit64() {
return bit32() + (bit32() << 1);
}


You try to change a variable twice, which is invalid, ie. undefined
behaviour.


There is a sequence point between the two modifications of x, so
there's no undefined behavior here. The C language doesn't specify
which of the two bit32()'s will be invoked first, so there is
unspecified behavior here. But the first two calls to bit32() are
going to return 1 and 2 respectively, giving:
1 + (2 << 1) which is 5, or
2 + (1 << 1) which is 4
for the first return of bit64(). (If this were undefined behavior, the
compiler would be free to do whatever it wanted. That's not the case
here -- he can reliably assume he'll get 4 or 5 ... he just can't
assume which one he'll get.)

(The two outputs he shows (1 and 2 for the first call to bit64())
aren't possible from the code he posted ... but I assume his test code
actually had "x++" instead of "++x".)

-- Brett
Nov 13 '05 #5
Brett Frankenberger <rb*@panix.co m> scribbled the following
on comp.lang.c:
In article <bf************ *@news.t-online.com>,
Jakob Bieling <ne*****@gmy.ne t> wrote:
"Oliver Brausch" <ob***@web.de > wrote in message
news:cd****** *************** ****@posting.go ogle.com...
static int x=0;
int bit32() {
return ++x;
}

int bit64() {
return bit32() + (bit32() << 1);
}
You try to change a variable twice, which is invalid, ie. undefined
behaviour.

There is a sequence point between the two modifications of x, so
there's no undefined behavior here. The C language doesn't specify
which of the two bit32()'s will be invoked first, so there is
unspecified behavior here. But the first two calls to bit32() are
going to return 1 and 2 respectively, giving:
1 + (2 << 1) which is 5, or
2 + (1 << 1) which is 4
for the first return of bit64(). (If this were undefined behavior, the
compiler would be free to do whatever it wanted. That's not the case
here -- he can reliably assume he'll get 4 or 5 ... he just can't
assume which one he'll get.) (The two outputs he shows (1 and 2 for the first call to bit64())
aren't possible from the code he posted ... but I assume his test code
actually had "x++" instead of "++x".)


Actually, yes they are. He has undefined behaviour - void main(). 1 and
2 are as legal outputs as 4 and 5, and so is "your mother was a hamster
and your father smelt of elderberries".

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"Life without ostriches is like coffee with milk."
- Mika P. Nieminen
Nov 13 '05 #6
In article <bf**********@o ravannahka.hels inki.fi>,
Joona I Palaste <pa*****@cc.hel sinki.fi> wrote:
Brett Frankenberger <rb*@panix.co m> scribbled the following
on comp.lang.c:
In article <bf************ *@news.t-online.com>,
Jakob Bieling <ne*****@gmy.ne t> wrote:
"Oliver Brausch" <ob***@web.de > wrote in message
news:cd***** *************** *****@posting.g oogle.com...
static int x=0;
int bit32() {
return ++x;
}

int bit64() {
return bit32() + (bit32() << 1);
}

You try to change a variable twice, which is invalid, ie. undefined
behaviour.

There is a sequence point between the two modifications of x, so
there's no undefined behavior here.


Actually, yes they are. He has undefined behaviour - void main(). 1 and
2 are as legal outputs as 4 and 5, and so is "your mother was a hamster
and your father smelt of elderberries".


Yes, of course, but I wasn't responding to that. I was responding to
the claim that calling bit32() twice, as shown above, was undefined
behavior. And it's not. I specifically didn't show the void main()
part of the code.

As a practical matter, if he fixed the void main() thing, he's not
going to get a different result -- he'll still get one of the two
possible outcomes from bit64(). So he'll end up with a program that
has unspecified (but not undefined) behavior.

-- Brett

Nov 13 '05 #7
On Sun, 20 Jul 2003 18:29:35 GMT, "Falcon Kirtarania" <cm****@shaw.ca >
wrote:
'void main' is illegal. It is *always* 'int main'.
No, it is not. However, int main has always been considered MUCH better
practice, because it allows for escapes and return codes.


No, it's illegal. Read the standards.

--
Be seeing you.
Nov 13 '05 #8
"Falcon Kirtarania" <cm****@shaw.ca > wrote in message
news:jc******** *************** @news1.calgary. shaw.ca...

"Jakob Bieling" <ne*****@gmy.ne t> wrote in message
news:bf******** *****@news.t-online.com...
"Oliver Brausch" <ob***@web.de > wrote in message
news:cd******** *************** **@posting.goog le.com...
have you ever heard about this MS-visual c compiler bug?
look at the small prog:


It is not a compiler bug. It is a bug in your code.
static int x=0;
int bit32() {
return ++x;
}

int bit64() {
return bit32() + (bit32() << 1);
}


You try to change a variable twice, which is invalid, ie. undefined
behaviour. Microsoft's compiler is absolutely right.
void main(int argc, char **argv) {


'void main' is illegal. It is *always* 'int main'.


No, it is not. However, int main has always been considered MUCH better
practice, because it allows for escapes and return codes.


You might want to do a search on Google about why you should write 'int
main' instead of 'void main'. Do not want to start such a long discussion
again. ;o)

regards
--
jb

(replace y with x if you want to reply by e-mail)
Nov 13 '05 #9
In article <cd************ *************@p osting.google.c om>,
ob***@web.de (Oliver Brausch) wrote:
Hello,

have you ever heard about this MS-visual c compiler bug?
look at the small prog:
static int x=0;
int bit32() {
return ++x;
}

int bit64() {
return bit32() + (bit32() << 1);
}

void main(int argc, char **argv) {
int i;
for (i = 0; i < 5; i++) printf("%d. %d\n", i, bit64());
}
Ok, the (correct) result is:

0. 2
1. 8
2. 14
3. 20
4. 26

This is what every compiled progam says. Inclusive MSVisualC Compiler
with Debug options or /Ot fast-option.

But do not dare to switch to the the /O2 option of MSVisualC Compiler.
Then once your computer cannot calculate anymore:

0. 1
1. 7
2. 13
3. 19
4. 25

So, up to Microsoft, 0 + 2 = 1 ?????. That's why their OS is so stable....
Try to increase the "<< 1". It even gets worse.

Ever seen this? I costed me hours of debugging. Can I
sue Microsoft for this?


Unspecified behaviour. It is absolutely to be expected that two
compilers will give different results. And I would just guess that the
code you posted is not the one that produced this output anyway; maybe
bit32 () contained an x++ instead of a ++x? If you took a programming
course with Microsoft, then maybe you can demand your money back.
Nov 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
7243
by: K. Shier | last post by:
when attempting to edit code in a class file, i see the bug "Visual Basic ..NET compiler is unable to recover from the following error: System Error &Hc0000005&(Visual Basic internal compiler error) Save your work and restart Visual Studio .NET." has anyone seen this bug and can you confirm one way or the other whether or not it can corrupt your source files? (by 'corrupt' i mean: do anything to it that will cause it to fail to load and...
5
3759
by: Anton Noll | last post by:
We are using Visual Studio 2003.NET (C++) for the development of our software in the fields digital signal processing and numerical acoustics. One of our programs was working correctly if we are using the Debug-Version of the program, but it fails (or leads to false results) if we are using the Release-Version. After a long debugging session we found out, that our program was working correctly, but the floating point processing...
3
1129
by: Robert Dickow | last post by:
Recently I'm experiencing a nasty problem in my development cycle: I compile my web application without errors in Visual Studio 2002 (C#, XP SP2). Then I test the web app in my Internet Explorer. All goes perfectly, performs as expected. Then, I'll continue to edit my code, recompile (successfully), and run IE again...whammo! IE hangs, VS hangs, All apps in system hang, Task Manager won't come up, and the only solution is to do a Vulcan...
0
1032
by: Hakuin | last post by:
Hello, I installed visual web developer 2005 express, and started to use it with mysql/odbc driver. It happens that when I open the database pane, and make some operations with the sql manager, the disk starts to make a horrible and prolonged sound of swapping. The app stucks for many seconds. I checked the disk activity with filemon, and there's a file sql.log getting a huge and nonsense activity. The same happened when trying to install...
0
7349
jwwicks
by: jwwicks | last post by:
Introduction This tutorial describes how to use Visual Studio to create a new C++ program, compile/run a program, resume work on an existing program and debug a program. It is aimed at the beginning CIS student who is struggling to get their programs working. I work in the computer lab at the college I'm attending and I see many students who don't know how to use the IDE for best results. Visual Studio automatically creates a number of...
0
10859
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10487
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9662
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8036
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7191
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5882
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4708
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4297
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.