The folowing code is giving a segfault when reaching the insert statement:
rna* annealer::anneal(rna strand1, rna strand2, const rna & opponent){
std::vector<nukleotid*>::iterator sit2;
std::vector<nukleotid*>::iterator eit2;
std::vector<nukleotid*>::iterator eit;
if(tryAnneal(strand1, strand2, opponent)) {
eit = strand1.getStrand().end();
sit2 = strand2.getStrand().begin();
eit2 = strand2.getStrand().end();
//here be segfault
strand1.getStrand().insert(eit, sit2, eit2);
strand1.isAnnealed = true;
rna* str = &strand1;
std::cout << *str << std::endl;
return str;
}
//...
return NULL;
}
rna contains a vector, which is returned by getStrand().
Its driving me crazy. I really cant understand why theres a segfault. A slightly different version of the code, in which in didnt declare new iterators, but was just passing strand1.getStrand().end(); (and the two other), to vector::insert() derictly threw a length_error which does not make any sense either, as my vectors are small (~10 elements).
can anyone see what am i doing wrong here?`
If
getStrand()returns avectorby value, thensit2andeit2are iterators into different copies of thevector, and both copies are destroyed as soon as you obtain the iterators. You need either to return thevectorby reference or save the copy of thevectorand obtain iterators from that one copy instead.