473,729 Members | 2,309 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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->getNextSomethi ng();
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->getNextSomethi ng();
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->getNextSomethi ng() 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 3331
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->getNextSomethi ng();
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->getNextSomethi ng();
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->getNextSomethi ng() 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******** **********@torn ado.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->getNextSomethi ng() 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_it erator
#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_b ack( 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_it erator<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
getNextSomethin g 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->getNextSomethi ng();
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->getNextSomethi ng();
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.
getNextSomethin g() return this structure refering to the next line that
match with the filter.
There's other problems.
In the first version p->getNextSomethi ng() 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 getNextSomethin g() 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->getNextSomethi ng();
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->getNextSomethi ng();
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.
getNextSomethin g() return this structure refering to the next line that
match with the filter.
There's other problems.
In the first version p->getNextSomethi ng() 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 getNextSomethin g() 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
2007
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 == "name2"){ do; something;
7
2544
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
2771
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 option, though that is an extremely manual and time-intensive approach. Thanks, -- Benjamin
10
4781
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 structures are read only) Ideally one should be able to put the redundant information there automatically so no mistakes are possible, but in a lot of case I see no way how to do it.
3
1520
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 VC7.1. I am experiencing VC7.1 C compile time taking about 4 times of VC6, though interestingly C++ stays within +10% margin. This is the same for debug, so it is not
5
1454
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 wait 10-50% longer...
3
2886
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
4844
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
5600
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
8917
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9281
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9200
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9142
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6722
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2680
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2163
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.