Can anyone explain this, bug in GCC????

  • Thread starter Joost Kraaijeveld
  • Start date
J

Joost Kraaijeveld

Hi,

Can anyone try to explain why on the line marked with "----->" the
variable "start" changes from it's original value to another value,
being const?

Example output (GCC 4.2 and 4.3 on Linux, 4.3 on Windows):

start 0: H
start 3: H
start 4: H
start 5a: A
start 5b: H


If you want the full source code:
http://trac.askesis.nl/svn/tidbits/AIAStar/Main.cpp

line 577 and further

1 file, can be compiled by gcc 4.2 /4.3 Windows/Linux with an installed
boost library


void findAStarCheapestPath( const Vertex& start,
const Vertex& finish,
Vertices pathVertices,
Edges pathEdges,
Vertices& allDiscoveredVertices,
Vertices& cheapestPathVertices,
Edges& cheapestPathEdges) const
{
Vertex localStart = start;
std::cout << "start 0: " << start << "\n";

pathVertices.insert( pathVertices.end(), /*start*/localStart);

Edges vertexEdges = getEdgesFor( /*start*/localStart);
Edges::iterator e = vertexEdges.begin();
while (e != vertexEdges.end())
{
const Vertex& nextVertex = (*e).otherSide( /*start*/localStart);
//std::cout << "start 1: " << /*start*/localStart << "\n";
if (nextVertex == finish )
{
pathEdges.insert( pathEdges.end(), (*e));
pathVertices.insert( pathVertices.end(), finish);
std::cout << "start 2: " << start << "\n";
break;
}
std::cout << "start 3: " << start << "\n";
if (std::find( allDiscoveredVertices.begin(),
allDiscoveredVertices.end(), nextVertex) == allDiscoveredVertices.end()
&&
std::find( pathVertices.begin(), pathVertices.end(), nextVertex) ==
pathVertices.end())
{
std::cout << "start 4: " << start << "\n";
allDiscoveredVertices.insert(allDiscoveredVertices.begin(), nextVertex);
------------------> std::cout << "start 5a: " << start << "\n";
std::cout << "start 5b: " << /*start*/localStart << "\n";
}


TIA

Joost
 
A

Alf P. Steinbach

* Joost Kraaijeveld:
Hi,

Can anyone try to explain why on the line marked with "----->" the
variable "start" changes from it's original value to another value,
being const?

Example output (GCC 4.2 and 4.3 on Linux, 4.3 on Windows):

start 0: H
start 3: H
start 4: H
start 5a: A
start 5b: H


If you want the full source code:
http://trac.askesis.nl/svn/tidbits/AIAStar/Main.cpp

line 577 and further

1 file, can be compiled by gcc 4.2 /4.3 Windows/Linux with an installed
boost library


void findAStarCheapestPath( const Vertex& start,
const Vertex& finish,
Vertices pathVertices,
Edges pathEdges,
Vertices& allDiscoveredVertices,
Vertices& cheapestPathVertices,
Edges& cheapestPathEdges) const
{
Vertex localStart = start;
std::cout << "start 0: " << start << "\n";

pathVertices.insert( pathVertices.end(), /*start*/localStart);

Edges vertexEdges = getEdgesFor( /*start*/localStart);
Edges::iterator e = vertexEdges.begin();
while (e != vertexEdges.end())
{
const Vertex& nextVertex = (*e).otherSide( /*start*/localStart);
//std::cout << "start 1: " << /*start*/localStart << "\n";
if (nextVertex == finish )
{
pathEdges.insert( pathEdges.end(), (*e));
pathVertices.insert( pathVertices.end(), finish);
std::cout << "start 2: " << start << "\n";
break;
}
std::cout << "start 3: " << start << "\n";
if (std::find( allDiscoveredVertices.begin(),
allDiscoveredVertices.end(), nextVertex) == allDiscoveredVertices.end()
&&
std::find( pathVertices.begin(), pathVertices.end(), nextVertex) ==
pathVertices.end())
{
std::cout << "start 4: " << start << "\n";
allDiscoveredVertices.insert(allDiscoveredVertices.begin(), nextVertex);
------------------> std::cout << "start 5a: " << start << "\n";
std::cout << "start 5b: " << /*start*/localStart << "\n";
}

'const' is only a promise that the routine won't change the object.

Have you considered aliasing.

Other than that, try to reduce the problem to a minimum, complete program and
post it.


Cheers & hth.,

- Alf
 
J

Joost Kraaijeveld

Alf said:
'const' is only a promise that the routine won't change the object.

Have you considered aliasing.

Could you explain what you mean by this?
Other than that, try to reduce the problem to a minimum, complete
program and post it.
That is why I send the link to the file along with the message, IMHO: a
shorter file would not make it any clearer, but I may be wrong...

TIA

Joost
 
D

Daniel Pitts

Joost said:
Could you explain what you mean by this?

That is why I send the link to the file along with the message, IMHO: a
shorter file would not make it any clearer, but I may be wrong...

Here is a shorter file that shows the same "bug" as your code, but it is
not a compiler bug.

#include <iostream>

struct foo {
int a;
};

struct bar {
foo f;
};

void moreStuff(bar &b) {
b.f.a -= 3;
}

void stuff(const foo &f, bar &b) {
using std::cout;
using std::endl;

cout << "First: " << f.a << endl;
moreStuff(b);
cout << "Second: " << f.a << endl;

}

int main(int argc, char**argv) {
bar b = {{7}};
stuff(b.f, b);
}
 
J

Joost Kraaijeveld

Daniel said:
Here is a shorter file that shows the same "bug" as your code, but it is
not a compiler bug.

Could you tell/suggest me what (exactly???) the point is , as I have
looked at it without any enlightment , sight....

TIA

Joost
 
D

Daniel Pitts

Joost said:
Could you tell/suggest me what (exactly???) the point is , as I have
looked at it without any enlightment , sight....

TIA

Joost
The point is that some *other* function is modifying the same instance.

It might be doing a const_cast to remove constness, or it might be
modifying a different (non-const) reference to that same instance.

You know that it changes right after that particular call. The problem
is in there. Look for anything that might modify any Vertex, and see if
there is a possibility that it is the same Vertex that you're looking at.
 
J

Joost Kraaijeveld

Daniel said:
The point is that some *other* function is modifying the same instance.

It might be doing a const_cast to remove constness, or it might be
modifying a different (non-const) reference to that same instance.

You know that it changes right after that particular call. The problem
is in there. Look for anything that might modify any Vertex, and see if
there is a possibility that it is the same Vertex that you're looking at.

Ah. That is not likely isn't it? The only thing that happens is that
something else (nextVertex) is put into
a vector (allDiscoveredVertices) after which the value is changed.....
Maybe (likely?) I am missing something though

Joost
 
T

Thomas J. Gritzan

Joost said:
Ah. That is not likely isn't it? The only thing that happens is that
something else (nextVertex) is put into
a vector (allDiscoveredVertices) after which the value is changed.....
Maybe (likely?) I am missing something though

If start (the value which changes) is a reference into this vector, then
a reallocation by this vector invalidates the reference.
 
J

Jim Langston

Joost Kraaijeveld said:
Hi,

Can anyone try to explain why on the line marked with "----->" the
variable "start" changes from it's original value to another value,
being const?

Example output (GCC 4.2 and 4.3 on Linux, 4.3 on Windows):

start 0: H
start 3: H
start 4: H
start 5a: A
start 5b: H
void findAStarCheapestPath( const Vertex& start,
const Vertex& finish,
Vertices pathVertices,
Edges pathEdges,
Vertices& allDiscoveredVertices,
Vertices& cheapestPathVertices,
Edges& cheapestPathEdges) const
{
{
std::cout << "start 4: " << start << "\n";
allDiscoveredVertices.insert(allDiscoveredVertices.begin(), nextVertex);

Here you are modifying a vector (presuming allDiscoveredVertices is a
vector) by inserting. This will change existing references as the memory
changes. Basically the vector is moved around in memory. Where it is moved
to and how is platform dependant. But the point is, once you do an insert
you can not count on existing references into the vector to be good anymore.
And your start is a reference to the start of the vector (still presuming
it's a vector, not sure how Vertex is defined).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top