strange bug when using gcc O2 or O3

Z

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?

Thanks for help.

zl2k
 
A

Alf P. Steinbach

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

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top