Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Can anyone explain this, bug in GCC????

Reply
Thread Tools

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

 
 
Joost Kraaijeveld
Guest
Posts: n/a
 
      02-09-2009
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
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      02-09-2009
* 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
 
Reply With Quote
 
 
 
 
Joost Kraaijeveld
Guest
Posts: n/a
 
      02-09-2009
Alf P. Steinbach wrote:
>
> '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
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      02-09-2009
Joost Kraaijeveld wrote:
> Alf P. Steinbach wrote:
>> '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...


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);
}

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
Joost Kraaijeveld
Guest
Posts: n/a
 
      02-09-2009
Daniel Pitts wrote:
> 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
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      02-09-2009
Joost Kraaijeveld wrote:
> Daniel Pitts wrote:
>> 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

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.



--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
Joost Kraaijeveld
Guest
Posts: n/a
 
      02-09-2009
Daniel Pitts wrote:
> Joost Kraaijeveld wrote:
>> Daniel Pitts wrote:
>>> 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

> 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







 
Reply With Quote
 
Thomas J. Gritzan
Guest
Posts: n/a
 
      02-09-2009
Joost Kraaijeveld schrieb:
> Daniel Pitts wrote:
>> Joost Kraaijeveld wrote:
>>> Daniel Pitts wrote:
>>>> 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

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


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

--
Thomas
 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      02-11-2009

"Joost Kraaijeveld" <(E-Mail Removed)> wrote in message
news:499080ba$0$3361$(E-Mail Removed)4al l.nl...
> 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
>
>

<SNIP>
>
> void findAStarCheapestPath( const Vertex& start,
> const Vertex& finish,
> Vertices pathVertices,
> Edges pathEdges,
> Vertices& allDiscoveredVertices,
> Vertices& cheapestPathVertices,
> Edges& cheapestPathEdges) const
> {

<SNIP>
> {
> 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).

> ------------------> std::cout << "start 5a: " << start << "\n";
> std::cout << "start 5b: " << /*start*/localStart << "\n";
> }
>
>
> TIA
>
> Joost


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Timer Not Working - Can Anyone Explain Why and How to fix? Sir Bill ASP .Net 5 01-12-2006 02:03 PM
Can anyone explain what this means to me... Wookie ASP .Net 2 11-13-2005 03:20 PM
IIS 6 security - anyone can explain this for me ? Michael Tsai ASP .Net 3 08-19-2005 03:05 AM
Can anyone explain me about this command? Keung Cisco 1 11-28-2004 11:20 AM
Newbie question: Can anyone explain to me how the As New works? renata ASP .Net 1 06-25-2003 12:26 PM



Advertisments