记性不好管理? 类成员(布尔值)大于1的值,在递归函数(Bad memory managemen

2019-10-16 16:41发布

我工作的一个子图匹配问题(分子内匹配化学官能团)。 原代码被写了另一名学生(在Visual C ++,没有MS特定的库),它在Windows上运行良好 。 然后,我添加了新功能的计划,但在运行时不改变算法子图匹配,并且在gcc4.2编译罚款的新方案/ Mac OS X的但是我遇到了奇怪的问题!

这是与此有关的对象和他们的成员:

  1. 原子:包含ID,要素,债券的列表(指针邦德对象的矢量),search_mark(布尔)。 函数来得到的变量和设置search_mark为真或假。

  2. 键:含有2个指针的数组,以原子A和B,并且当与参数原子* B调用,反之亦然返回原子* A的函数。

  3. 分子:包含指向的矢量的原子,和函数来获得使用任一原子ID或载体内的位置的原子*。

  4. 原子构成的子类:HammettAtom。 它包含的额外的构件是一个原子指针相关分子原子。

这是递归函数的算法:对于在阵列中每一个原子,比较数组B中的原子(哈米特基团,通常约10-20个原子大小)。 如果该元素是相同的,获得每个的连接原子的列表,并重复。 所测试的原子沿途标记,所以在一个点上不会有更多的未标记的连接原子。

下面是代码(不变 ,仅COUT位由我测试添加)。 当函数被首先调用,第一个载体是从测试分子的单个原子,所述第二载体是哈米特组分子的第二原子。 (在哈米特的第一原子具有ID“X”,并且可以是任何东西。)

bool HammettCheck::checkSubproblem(vector<Atom*> bonded_atoms, vector<Atom*> my_list) 
{
unsigned int truth=0;
vector<Atom*> unmarked_bonded;
vector<Atom*> unmarked_list;
cout << "\n size of Hammett array: " <<my_list.size()<< " size of mol array: "<< bonded_atoms.size() << endl; //for testing
//If number of connected atoms is different, return false.
if( bonded_atoms.size() != my_list.size() ){
    return false;
}

//Create new lists.
for(unsigned int i=0; i < bonded_atoms.size() ; i++){

    //Create list of unmarked connected atoms in molecule.
    if( !bonded_atoms[i]->isMarked() ){
        unmarked_bonded.push_back(bonded_atoms[i]);
    }

    //Create list of unmarked connected atoms in hammett.
    if( !my_list[i]->isMarked() ){
        unmarked_list.push_back( my_list[i] );
    }
}
cout << "size of unmarked Hammett array: " << unmarked_list.size() << " size of unmarked mol array: "<< unmarked_bonded.size() <<endl; //for testing
//If number of unmarked connected atoms is different, return false.
if( unmarked_bonded.size() != unmarked_list.size() ){
    return false;
}


//Check each unmarked atom connected in the molecule against possible atoms it could be in the hammett group.
for(unsigned int i=0; i < unmarked_bonded.size(); i++){
  cout<< "atom in um_mol array considered ID: " << unmarked_bonded[i]->getID() << " Ele: " << unmarked_bonded[i]->getEle()<< endl;
    /*Unmarked hammett assigned in reverse order so that the undefined "X" atom is only 
      assigned if a connected atom can not possibly be any other atom.*/
    for(int j=(unmarked_list.size()-1); j > -1; j--){
      cout << "atom in um_h_array considered ID: " << unmarked_list[j]->getID() << endl;
        //If hammett atom has already been assigned to a connected atom, it cannot be assigned to another
        if(!unmarked_list[j]->isMarked()){
          cout << unmarked_list[j]->getID() << "is unmarked" <<endl;
            /*If connected atom could only be hammett group's connection 
              to the rest of the molecule, assign it as such.*/
            if( !strcmp(unmarked_list[j]->getEle().c_str(), "X") ){
                unmarked_bonded[i]->mark();
                unmarked_list[j]->mark(unmarked_bonded[i]);
                truth++;
                cout<< "mol atom ID "<< unmarked_bonded[i]->getID() <<" marked as X, current truth: "<< truth << endl;
                cout << unmarked_list[j]->getID() << "is now marked(1)/unmarked(0) " << unmarked_list[j]->isMarked() << " and break loop "<<endl;
                break;
            }

            /*If connected atom is the same element as a possible hammett atom,
              check that atoms connections by running them through the subproblem.*/
            if( !strcmp(unmarked_bonded[i]->getEle().c_str(), unmarked_list[j]->getEle().c_str()) ){
                unmarked_bonded[i]->mark();
                unmarked_list[j]->mark(unmarked_bonded[i]);
                cout<<"found same ele between mol_id "<< unmarked_bonded[i]->getID() <<" and ham_id " << unmarked_list[j]->getID() <<endl;
                vector<Atom*> new_bonded = getAttachedAtoms( unmarked_bonded[i] );
                vector<Atom*> new_list = getAttachedAtoms( unmarked_list[j] );
                if( checkSubproblem( new_bonded, new_list ) ){
                  cout<<"found same atom"<<endl;
                    truth++;
                    break;

                /*If only the elements are the same do not assign 
                  the hammett atom to this connected atom.*/
                }else{
                    unmarked_bonded[i]->demark();
                    unmarked_list[j]->demark();
                }
            }
        }
    }
}

//Return true if all connected atoms can be assigned atoms of the hammett group.
if( truth == unmarked_bonded.size() ){
    return true;
}else{
    return false;
}
}

我用29个原子的测试分子运行编译程序,它与两个哈米特组。 它应该包含2组而不是组1。然而,每当我开始与2个原子是有相同的元素返回真。 下面是输出的一个示例(在分子实际上不包含哈米特组在该原子)

 currently at molecule atom ID 1

 size of Hammett array: 1 size of mol array: 1
 size of unmarked H array: 1 size of unmarked mol array: 1
 atom in um_mol array considered ID: 1 Ele: N
 atom in um_h_array considered ID: N1
 N1is unmarked
 found same ele between mol_id 1 and ham_id N1

 size of Hammett array: 3 size of mol array: 3
 size of unmarked H array: 3 size of unmarked mol array: 3
 atom in um_mol array considered ID: 2 Ele: H
 atom in um_h_array considered ID: O2
 O2is unmarked
 atom in um_h_array considered ID: O1
 O1is unmarked
 atom in um_h_array considered ID: X
 X is unmarked
 mol atom ID 2 marked as X, current truth: 1
 X is now marked(1)/unmarked(0) 128 and break loop 
 atom in um_mol array considered ID: 8 Ele: C
 atom in um_h_array considered ID: O2
 O2is unmarked
 atom in um_h_array considered ID: O1
 O1is unmarked
 atom in um_h_array considered ID: X
 X is unmarked
 mol atom ID 8 marked as X, current truth: 2
 X is now marked(1)/unmarked(0) 160 and break loop 
 atom in um_mol array considered ID: 17 Ele: C
 atom in um_h_array considered ID: O2
 O2is unmarked
 atom in um_h_array considered ID: O1
 O1is unmarked
 atom in um_h_array considered ID: X
 X is unmarked
 mol atom ID 17 marked as X, current truth: 3
 X is now marked(1)/unmarked(0) 128 and break loop 
 found same atom
 Hammet group 2 checkSubproblem true
 Hammett added to atom 1

很抱歉,如果那是很久。 但问题是,右后,我已经打上了“X”原子(在哈米特分子1原子),并试图让search_mark布尔值,它有一个值大于1所以X是错误的“标记”几次较大 “真理”柜台上去,待病情真相== unmarked_bonded.size()达到。

我不知道实际的问题是什么? 价值128提出了一些混合内存/指针的问题,但我不确定如何找到了这一点。 我甚至不知道它有什么用递归函数!

我会很感激,如果任何人都可以建议的东西,我可以试试。 提前致谢!

PS为凌动类函数的代码。

 string Atom::getID()
{
return id;
}

string Atom::getEle()
{
return ele;
}
void Atom::mark()
{
search_mark = true;
}

void Atom::demark()
{
search_mark = false;
}
void HammettAtom::mark(Atom* assigned)
{
search_mark = true;
related_mol_atom = assigned;
}
bool Atom::isMarked()
{
return search_mark;
}

Answer 1:

感谢所有的建议。 Valgrind的下运行的程序后,很显然,这个问题是涉及到内存泄漏。 我搬到分配器的位置确定的“deinitely丢失”的问题,而search_mark问题似乎被删除。 该程序现在运行正常,但与内存泄漏。 我没有设法虽然解决内存泄漏问题,我已经发布了一个新的问题在这里 。



文章来源: Bad memory management? Class member (boolean) value greater than 1, in recursion function