472,785 Members | 1,221 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

compile time optimization problem

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

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
Nov 3 '05 #1
5 3228
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'.
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.
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.
The standard say about the behaviour of compiler that can be optimize
the loop.
Uh... What?
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.
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.

Victor
Nov 4 '05 #2

"Carmine Cairo" <ca******@gmail.com> wrote in message
news:LR******************@tornado.fastwebnet.it...
| 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;

welldone doesn't exist.

| size = p->getSize();

size, p and getSize() don't exist.

| backbone = new rightType[size];

nothing on that line exists either. etc...

<snip>

| cout << "GREAT! Another welldone." << endl;

neither cout nor endl exist.

<snip>

| In the first version p->getNextSomething() it's called ALWAYS one time
| and the backbone vector isn't filled as well.

backbone is a vector? ok, a vector of what?

|
| 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?

if backbone is a vector of integers, for example, then please help us
help you:

#include <iostream>
#include <ostream> // std::cout, std::endl
#include <iterator> // std::ostream_iterator
#include <algorithm> // std::copy(...)
#include <vector>

int main()
{
const int size(10); // modify to your heart's desire
std::vector< int > backbone;

for (int i = 0; i < size; ++i)
{
backbone.push_back( i ); // one element at a time
}
std::cout << "the vector's size = " << backbone.size();
std::cout << std::endl;

std::copy( backbone.begin(),
backbone.end(),
std::ostream_iterator<int>( std::cout, " ") );
std::cout << std::endl;

// now develop your question...
// C++ programmers are reknown for their poor guessing ability
// they even have a name for the disease: UB
return 0;
}

/*
the vector's size = 10
0 1 2 3 4 5 6 7 8 9

*/
Nov 4 '05 #3
>
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?


You have a bug in your code, don't blame the compiler, look for the bug.
The bug will be somewhere completely unexpected, perhaps in the
getNextSomething method.

john
Nov 4 '05 #4
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
Nov 4 '05 #5
The problem it's solved :)

no human error ed no machine error.
To try to proof the compiler error I start to write an equivalent piece
of code, then compile it and "see" what's happen.
The reason it's obviously the size of my code who is too much longer for
a complete post on this NG. But when I try to compile the smaller one it
worked perfectly. Then I try another time to compile my code but the
result don't change. With cout all work properly, without all fails.
Then I try to download and compile the code with C++ Builder and all
works fine.
Then I start to think really about a bug in g++ (I use the cygwin
version of g++). Then I remove from my hard drive the cygwin and
(re)install it completely. Then i retry to compile the original code and
now ALL WORK FINE.
So, no error in allocation mechanism, no memory overrun, and no error
was found on my code. No error and no bug it's present in g++ (cygwin
version) but only some undefined installation failure who caused this
strange behavior.

Now I'll come back to use the gnu-gcc package but if I see something
strange behavior ... I reinstall and retry :)

Thanks to all
Carmine
Carmine Cairo wrote:
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

Nov 7 '05 #6

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

Similar topics

11
by: Markus Dehmann | last post by:
I have a big class that contains code like this (the code is automatically generated according to some configuration): if(str == "name1"){ do; something; name1_specific; }else if(str ==...
7
by: S.K.Mody | last post by:
typedef unsigned long long int uint64 typedef unsigned char uint8; Class Simple { union { uint64 x; uint8 r; } public: Simple(uint64 n) : x(n) {;} //....
15
by: Benjamin Rutt | last post by:
Are there any C tools that can find redundant #includes in a project, so as to shorten compile time? Of course, eliminating single #includes by hand and determining if the recompile fails is one...
10
by: Bart Goeman | last post by:
Hi, I have a question about how to put redundant information in data structures, initialized at compile time. This is often necessary for performance reasons and can't be done at run time (data...
3
by: Ys | last post by:
Hello, I have just migrated my project from VC6 to VC7.1. The project is mostly written in C, and some in C++ without MFC (Console application). I am on Win2k. I am not using managed code in...
5
by: Michael | last post by:
i experience slower compile times with VC++ 2003 compared to VC+6.0. Anyone experiencing the same? Should that be expected? This ineed matters, when total compilation time is > 1h and you have to...
3
by: Jason S | last post by:
is there any way to use templates to bind integer/floating point constants to a template for compile-time use? e.g. template <double conversion> class meters { const factor = conversion;
15
by: steve yee | last post by:
i want to detect if the compile is 32 bits or 64 bits in the source code itself. so different code are compiled respectively. how to do this?
27
by: CodeMonk3y | last post by:
gotta question on sizeof keyword does the sizeof keyword calcuates the size at compile time or run time ?? -- Posted on news://freenews.netfront.net - Complaints to news@netfront.net --
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.