473,395 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

strange bug when using gcc O2 or O3

hi, all
I am not sure of this question is suitable for the c++ language group
but still hope the experts here can give me some hints.

I am using gcc 3.4.4 in fc3 and got a very strang error when using
optimization O2 or O3 (the program runs fine if not using
optimization).

Very strange, I can avoid the error by writing in this way:

unsinged long a;
.....
func(const unsigned long a){
if (a == 123){
code_A
}

else{
code_A

}
}

code_A is a block of code which will add a into a STL vector under
certain conditions. As you can see, the if...else is totally noncensse
since both code_A are exactly the same. However, if I don't write in
this way, for example

unsigned long a;
....
func(const unsigned long a){
code_A

}

the gcc optimization will add a different number other than "123" to
the vector sometimes and I am not able to track the error using DDD.
The break point won't break at where it is set!

I found the problem that sometimes the value added into the vector is
not what I expected, say, I got "456" when I tried to add "123". Then I
tried to debug the block of code_A to see what's going on if "123" is
added. So I use the if...else. The bug dissapper just by adding the
if...else. Is gcc -O2 usuable? What the problem is that?

Thanks for help.

zl2k

Jul 26 '06 #1
1 2139
* zl2k:
hi, all
I am not sure of this question is suitable for the c++ language group
but still hope the experts here can give me some hints.

I am using gcc 3.4.4 in fc3 and got a very strang error when using
optimization O2 or O3 (the program runs fine if not using
optimization).

Very strange, I can avoid the error by writing in this way:

unsinged long a;
....
func(const unsigned long a){
if (a == 123){
code_A
}

else{
code_A

}
}

code_A is a block of code which will add a into a STL vector under
certain conditions. As you can see, the if...else is totally noncensse
since both code_A are exactly the same. However, if I don't write in
this way, for example

unsigned long a;
...
func(const unsigned long a){
code_A

}

the gcc optimization will add a different number other than "123" to
the vector sometimes and I am not able to track the error using DDD.
The break point won't break at where it is set!

I found the problem that sometimes the value added into the vector is
not what I expected, say, I got "456" when I tried to add "123". Then I
tried to debug the block of code_A to see what's going on if "123" is
added. So I use the if...else. The bug dissapper just by adding the
if...else. Is gcc -O2 usuable? What the problem is that?
The rewrite seems to disable optimization by making the choice too hard
for the compiler to predict.

Most probably the problem is in your code_A, not in the compiler.

I suggest you post a small example program that exhibits the problem.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 26 '06 #2

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

Similar topics

24
by: David | last post by:
hello. when doing the simple following computation, the value put into the variable numMinusOne is NOT the same as what the computation is showed to be in the Watch window!! here is the code:...
3
by: Bill C. | last post by:
Hi, I've got a simple console app that just reads an XML file into a DataSet then prints out a description of each table in the DataSet, including column names and row values for each column. ...
9
by: Wescotte | last post by:
Here is a small sample program I wrote in PHP to help illustrates problem I'm having. The data base is using DB2 V5R3M0. The client is WinXP machine using the iSeries Client Access Driver ver...
25
by: Neil Ginsberg | last post by:
I have a strange situation with my Access 2000 database. I have code in the database which has worked fine for years, and now all of a sudden doesn't work fine on one or two of my client's...
0
by: unknown | last post by:
Hi, I am developing an online book store with shopping cart. My shopping cart is represented as a Xml server control and I am using an XSLT to render it at the client side. I am using an...
2
by: Buddy Ackerman | last post by:
I have a web app that I have setup on numerous web servers. I've set one up for a new client at their hosting facility and cannot get it to connect to their database. I get a "SQL Server does not...
8
by: Spam Trap | last post by:
I am getting strange resizing problems when using an inherited form. Controls are moving themselves seemingly randomly, but reproducibly. "frmBase" is my base class (a windows form), and...
11
by: Martin Joergensen | last post by:
Hi, I've encountered a really, *really*, REALLY strange error :-) I have a for-loop and after 8 runs I get strange results...... I mean: A really strange result.... I'm calculating...
5
by: Developer.Man4 | last post by:
when i try using HttpContext.Current.Server.MapPath(path) from a desktop application built using c# i get a System.NullReferenceException can anyone tell me why and what to do?? m using...
4
by: John Brock | last post by:
I have a .NET application that, among other things, creates Excel workbooks, and I have run into a very strange problem involving formulas on one worksheet that reference values on another...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...

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.