Help with STL

Giovani Faccin faccin.giovani@gmail.com
Tue Sep 9 18:25:00 GMT 2008


Hi all!

I'm quite puzzled by this, perhaps one of the gurus on the list might
be able to help ;)

I have this member function inside a class:


calc_atom_forc(Atom_Classical *atm, std::list<Atom_Classical*> *neighbours)


Where the Atom_Classical object is essentially a struct holding data
plus a few member functions. The neighbours pointer points to a
std::list of pointers to Atom_Classical objects that are stored
outside this class.

Now situation "A". Let's say that right upon entering the
calc_atom_forc routine, I run this loop:

calc_atom_forc(Atom_Classical *atm, std::list<Atom_Classical*> *neighbours)
{
    for ( std::list<Atom_Classical*>::iterator a =
neighbours->begin(); a != neighbours->end(); a++)
    {
        cout << (*a)->x() << endl;
    };
}

This will work fine, and the program will print the value returned by
the x() function of each Atom_Classical object. So far so good.

Now we get into situation B. I need to manipulate a bit those
pointers. This is the idea:
calc_atom_forc(Atom_Classical *atm, std::list<Atom_Classical*> *neighbours)
{
        //Allocating a vector that will hold the guys we need to
change inside this routine
	std::vector<Atom_Classical> changed_atms;

	for (std::list<Atom_Classical*>::iterator neigh =
neighbours->begin(); neigh != neighbours->end(); neigh++)
	{	
               If ( criteria_that_does_not_matter_now == true)
               {
                        //Pushing a copy of the original
Atom_Classical pointed by iterator neigh into the changed_atms vector:
                        changed_atms.push_back( *(*neigh) );
                        //Printing debug info:
                        cout << "Before changing atom " <<
(*neigh)->id() << " we had: " << (*neigh)->posi() << endl;

                        //Removed here some code that would change the
data on the "changed_atms[changed_atms.size() -1]" object

			//Now we'll make the corresponding neigh pointer point to the
modified atom on the newly created changed_atoms vector, instead of
the atom in the original neighbours list:
			(*neigh) = &(changed_atms[changed_atms.size() -1]);
                        //Printing debug info:
                        cout << "After changing atom " <<
(*neigh)->id() << " we have: " << (*neigh)->posi() << endl;
		};
	};
}

This also works fine. So in situation B, I'm having some of the
pointers pointed by the neighbours list point to the objects inside
the changed_atms container I've created inside this function. Notice
that the neighbours list is not allocated inside this function, all I
have is a pointer to it. I know that when the function ends, the
original neighbours list will have invalid pointers. That does not
matter, because that list will be deleted as soon as this routine
ends.

Here comes the trouble. What I need to do is run situation B, then
situation A. If I do this, I get a segfault error. This is how it's
like:

calc_atom_forc(Atom_Classical *atm, std::list<Atom_Classical*> *neighbours)
{
	// START OF CODE FROM SITUATION B, IDENTICAL TO THE ONE SHOWN PREVIOUSLY:

        //Allocating a vector that will hold the guys we need to
change inside this routine
	std::vector<Atom_Classical> changed_atms;

	for (std::list<Atom_Classical*>::iterator neigh =
neighbours->begin(); neigh != neighbours->end(); neigh++)
	{	
               If ( criteria_that_does_not_matter_now == true)
               {
                        //Pushing a copy of the original
Atom_Classical pointed by iterator neigh into the changed_atms vector:
                        changed_atms.push_back( *(*neigh) );
                        //Printing debug info:
                        cout << "Before changing atom " <<
(*neigh)->id() << " we had: " << (*neigh)->posi() << endl;

                        //Removed here some code that would change the
data on the "changed_atms[changed_atms.size() -1]" object

			//Now we'll make the corresponding neigh pointer point to the
modified atom on the newly created changed_atoms vector, instead of
the atom in the original neighbours list:
			(*neigh) = &(changed_atms[changed_atms.size() -1]);
                        //Printing debug info:
                        cout << "After changing atom " <<
(*neigh)->id() << " we have: " << (*neigh)->posi() << endl;
		};
	};

       // END OF CODE FROM SITUATION B.
       // START OF CODE FROM SITUATION A, IDENTICAL TO THE ONE SHOWN PREVIOUSLY:

       for ( std::list<Atom_Classical*>::iterator a =
neighbours->begin(); a != neighbours->end(); a++)
       {
            cout << (*a)->x() << endl;   --> Program crashes upon
reaching this line for the first time.
       };

       // END OF CODE FROM SITUATION A.
}

What is so strange here is that I can test the code in part B, and
it's working (I can see it through the couts on the screen). Then I
get on part A and get a segfault while accessing the same list that
had worked fine just a few lines before.

What's wrong here?

Thank you very much!

Giovani Faccin



More information about the Gcc-help mailing list