gpt4 book ai didi

c++ - 内存管理不好?大于 1 的类成员( bool )值,在递归函数中

转载 作者:太空宇宙 更新时间:2023-11-04 12:09:21 24 4
gpt4 key购买 nike

我正在研究子图匹配问题(匹配分子内的化学官能团)。原始代码由另一名学生编写(在 Visual C++ 下,没有 MS 特定库)并且它在 Windows 上运行良好。然后我在程序中添加了新函数,但没有改变子图匹配的算法,新程序在 gcc4.2/Mac OS X 下编译得很好。但是我在运行时遇到了奇怪的问题!

此处相关的对象及其成员:

  1. Atom:包含 ID、元素、Bonds 列表(指向 Bond 对象的指针的 vector )、search_mark (bool)。获取变量并将 search_mark 设置为真或假的函数。

  2. Bond:包含一个由 2 个指向原子 A 和 B 的指针组成的数组,以及一个在使用参数 atom* B 调用时返回 atom* A 的函数,反之亦然。

  3. 分子:包含指向原子的指针 vector ,以及使用原子 ID 或 vector 中的位置获取原子*的函数。

  4. Atom 的子类:HammettAtom。它包含的额外成员是指向相关分子原子的原子指针。

这是递归函数的算法:对于数组 A 中的每个原子,与数组 B 中的一个原子(Hammett 组,通常大小约为 10-20 个原子)进行比较。如果元素相同,则获取每个元素的连接原子列表,然后重复。被测试的原子沿途被标记,所以在某一时刻不会再有未标记的连接原子。

这是代码(未更改,我只添加了 cout 位用于测试)。首次调用该函数时,第一个 vector 是来自测试分子的单个原子,第二个 vector 是哈米特基团分子的第二个原子。 (Hammett 中的第一个原子的 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 个原子的测试分子运行编译程序,并将其与两个 Hammett 组进行比较。它应该包含第 2 组而不是第 1 组。但是,只要我从 2 个具有相同元素的原子开始,它就会返回 true。以下是输出示例(该分子在该原子处实际上不包含 Hammett 基团)

 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”原子(Hammett 分子中的第一个原子)并尝试获取 search_mark bool 值之后,它的值大于 1。因此 X 是错误的“标记”几次,“真相”计数器上升,直到达到条件 truth == unmarked_bonded.size()。

我不确定实际问题是什么?值 128 表明存在一些混淆的内存/指针问题,但我不确定如何找到它。我什至不确定它是否与递归函数有关!

如果有人能提出我可以尝试的建议,我将不胜感激。提前致谢!

附言Atom 类函数的代码。

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

最佳答案

感谢所有建议。在 Valgrind 下运行程序后,很明显问题与内存泄漏有关。我移动了分配器在确定的“绝对丢失”问题中的位置,并且 search_mark 问题似乎已被删除。该程序现在按预期运行,但存在内存泄漏。我还没有设法解决内存泄漏问题,我发布了一个新问题 here .

关于c++ - 内存管理不好?大于 1 的类成员( bool )值,在递归函数中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10543987/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com