C
Carmine Cairo
Hi,
I'm working on a project and today I've note a little problem during the
compile fase.
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 = p->getNextSomething();
if(backbone.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.
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 = p->getNextSomething();
if(backbone.error == NO_ERROR) {
welldone++;
cout << "GREAT! Another welldone." << endl;
}
}
somethingelse = new someType[welldone];
now the welldone value is the expected one.
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.
The standard say about the behaviour of compiler that can be optimize
the loop. 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?
Does something of you have encontered the same problem?
I use the GNU GCC as compiler. I try to use the -O0 flag but don't work.
Any suggest?
Thanks in advance
Carmine
I'm working on a project and today I've note a little problem during the
compile fase.
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 = p->getNextSomething();
if(backbone.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.
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 = p->getNextSomething();
if(backbone.error == NO_ERROR) {
welldone++;
cout << "GREAT! Another welldone." << endl;
}
}
somethingelse = new someType[welldone];
now the welldone value is the expected one.
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.
The standard say about the behaviour of compiler that can be optimize
the loop. 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?
Does something of you have encontered the same problem?
I use the GNU GCC as compiler. I try to use the -O0 flag but don't work.
Any suggest?
Thanks in advance
Carmine