Victor Bazarov wrote:
Carmine Cairo wrote: I'm working on a project and today I've note a little problem during
the compile fase.
That doesn't seem right. If you didn't run it, how could have you
known that 'welldone' is always 1, regardless of the input? You had
to have run it to notice the problem.
Here a little piece of code:
// 1st version
welldone = 0;
size = p->getSize();
backbone = new rightType[size];
p->rewind();
for (int i = 0; i < size; i++) {
backbone[i] = p->getNextSomething();
if(backbone[i].error == NO_ERROR) welldone++;
}
somethingelse = new someType[welldone];
if I try to compile and run this code, at the end of for loop the
value of welldone is ALWAYS 1 excepted when size is 0.
That means at least once 'backbone[i].error' equals 'NO_ERROR'.
I check it with debugger and with some cout inserted into code just for
debug information and I've also create an input ad hoc in witch all
backbone[i].error are equals to NO_ERROR.
When I say ALWAY means really ALWAY even when it isn't the expected
result.
Now, try to add a simple cout into a loop:
// 2nd version
welldone = 0;
size = p->getSize();
backbone = new rightType[size];
p->rewind();
for (int i = 0; i < size; i++) {
backbone[i] = p->getNextSomething();
if(backbone[i].error == NO_ERROR) {
welldone++;
cout << "GREAT! Another welldone." << endl;
}
}
somethingelse = new someType[welldone];
now the welldone value is the expected one.
OK, I'll have to believe you, since the code is incomplete and you
don't even describe the input you're giving your program.
p is an istance of a class that menage a vector<string>, each element of
a vector give me some information about a protein structure.
Before the piece of code there are some setting for select the line I'm
interested to processing, something like this:
p->setAtomFilter("C");
p->setChainFilter("E");
etc...
rightType it's a sctructure that store 3D coordinate, an error flag, ad
other two minor things.
getNextSomething() return this structure refering to the next line that
match with the filter.
There's other problems.
In the first version p->getNextSomething() it's called ALWAYS one time
and the backbone vector isn't filled as well.
Sounds very much like a memory overrun. Undefined behavour of sorts.
Yes, it's the first think I've try to discover.
But every time I insert the cout all run done.
With the debbugger I try to discover some memory overrun but when I use
it the cicle it's performed size time and getNextSomething() it's called
size time. :-(
The standard say about the behaviour of compiler that can be optimize
the loop.
Uh... What?
if you write a for loop like this:
int i, k;
for (i = 0; i < 100; i++) {
k++;
}
the compiler optimize this loop and set directly i and k at 100 without
done any loop. It's a right choose, this loop can be serve only for CPU
cosuming time.
When I put in the loop che cout the compiler can't act the
optimization (there's something to print). But why the compiler don't
"see" the use of i as index of a vector? And why the compiler don't
"see" the use of welldone for a future allocation?
Erm... I don't know how to answer those questions. You seem pretty
convinced it's the compiler's fault. I cannot blindly accept that.
In 99.9% of cases when the programmer tries to blame the compiler, it
is his/her own fault.
I agree with you, I don't want blame the compiler, I'am try to know why
it have this behaviour with this code.
Today I've try to compile the code on a different version of g++ and it
work well.
The "wrong optimization" it's done when I use GCC 3.4.4 (cygwin special)
(gdc 0.12, using dmd 0.125).
The right run it's done when I use GCC version 3.3.5 (Debian
1:3.3.5-8ubuntu2)
Does something of you have encontered the same problem?
Something of us may have encountered some strangeness connected to the
behaviour of the optimizer, but there is no indication that it's what
you're experiencing.
I use the GNU GCC as compiler. I try to use the -O0 flag but don't
work. Any suggest?
Suggest: read FAQ 5.8.
Thanks a lot.
Carmine