0
결함의 원인이 독립적 목록 사이 문을 지우고했다 및 B, 어느 시점에서 나는리스트 A에서 목록 B로 1 요소를 이동해야합니다.이 개체는 현재 위치에있는 목록의 위치 (반복자)를 기억합니다. 그것은 1 개의리스트, 삽입 및 지우기에 대해 완벽하게 잘 작동합니다. 그럼에도 불구하고 목록 A의 반복주기 내에서 목록 B를 변경하면 삽입 및 지우기 지침을 실행하는 순서에 따라 seg 오류가 발생하는지 결정됩니다.순서는/문제를 복제가 가능한 간단한 예를 SEG 내가 뭘 구현하고하는 것은 그래서 코드를 제거 한 좀 더 복잡하다
typedef struct AO_ {
int id;
list<AO_*>::iterator thispos;
} AnObject;
class MyList {
public:
list<AnObject*> ll;
int sizell;
MyList(){ sizell=0; }
void insert(AnObject* AnObjecti){
AnObjecti->thispos= ll.insert(ll.end(),AnObjecti);
sizell++;
}
list<AnObject*>::iterator remove(AnObject* AnObjecti){
return ll.erase(AnObjecti->thispos);
}
void print(){
cout << "contents:";
list<AnObject*>::iterator itAux;
for (itAux=ll.begin(); itAux!=ll.end(); itAux++)
cout << " " << (*itAux)->id;
cout << endl;
}
};
int main(int argc, char *argv[]){
MyList* listA= new MyList();
MyList* listB= new MyList();
AnObject* objAux= new AnObject();
for(int i=0; i<10; i++){
objAux= new AnObject();
objAux->id= i;
listA->insert(objAux);
}
cout << "list A:" << endl; listA->print();
list<AnObject*>::iterator it= listA->ll.begin();
while(it!=listA->ll.end()){
objAux= (*it);
if(objAux->id==2){
//listB->insert(objAux); //***THIS CAN GO HERE (seg fault on next cycle)
it= listA->remove(objAux);
listB->insert(objAux); //***OR THIS CAN GO HERE (all ok)
}
else
++it;
}
cout << "list A:"; listA->print();
cout << "list B:"; listB->print();
}
출력 : 교환의 지시에 누군가가 이유를 알고 있나요 * 로 표시하는 경우
list A:
contents: 0 1 2 3 4 5 6 7 8 9
list A:contents: 0 1 3 4 5 6 7 8 9
list B:contents: 2
내가 독방 감금 오류를 얻을 여기 코드는?
listB->insert
에 대한 호출이
objAux->thispos
을 수정 사전 호세