recursive function on a map of objects segfaults when i pass by value OR reference

Hopefully someone can shed some light on this one for me, since this is well beyond my limited programming experience. I am working on a program that reads a tree from bottom to top and performs calculations every time it reaches a node. The tree is represented by a map of leaf/node objects, where each node specifies what its children are. Initially only the leaves of the tree have any values associated with them, so each parent node builds on them. I am trying to write a recursive function that take the map as a parameter and fill in the values as it goes.

Given what I want to do I thought that I should pass the map by reference so that the changes in the function will affect the changes in the map passed to it. But when I did this, the program segfaults when I try to print values of the first node.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
void findParsimony(string s, std::map <string, Node>& tr,int len){
if(tr[s].getChild(0)=="NA"){cout<< s <<"\n\n";}//base case is when function reaches a leaf
else{
	findParsimony(tr[s].getChild(0),tr,len); //pass left branch
	findParsimony(tr[s].getChild(1),tr,len); //pass right branch
	cout << "got here for " << s << endl;
	for(int i=0;i<len;i++){	
		if(tr[tr[s].getChild(0)].getSeqAt(i)==tr[tr[s].getChild(1)].getSeqAt(i)){		
			tr[s].setTrace(i,'L');
			tr[s].setSeq(i, tr[tr[s].getChild(0)].getSeqAt(i) );
			tr[s].setScore(i, min( tr[tr[s].getChild(0)].getScoreAt(i), tr[tr[s].getChild(1)].getScoreAt(i) ) );
		}
		else{	
			if(tr[tr[s].getChild(0)].getScoreAt(i)+1 < tr[tr[s].getChild(1)].getScoreAt(i)+1){
				tr[s].setTrace(i,'L');
				tr[s].setSeq(i, tr[tr[s].getChild(0)].getSeqAt(i) );
				tr[s].setScore(i, tr[tr[s].getChild(0)].getScoreAt(i)+1 );	
			}
			else{
				tr[s].setTrace(i,'R');
				tr[s].setSeq(i, tr[tr[s].getChild(1)].getSeqAt(i) );
				tr[s].setScore(i, tr[tr[s].getChild(1)].getScoreAt(i)+1 );	
			}
		}
	}
}
}


Interestingly, when I tried passing the map by value instead, I also wind up with a segfault, but it happens at a higher node in the tree (one node higher, to be exact). So the problem is arising because values that I calculated for a node previously are somehow not being stored, but I'm not sure how I would fix this. Thanks in advance for your ideas.
Topic archived. No new replies allowed.