473,406 Members | 2,956 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,406 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 3291
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.