segmentetion fault......again.

About five days ago i posted here a piece of code that produced a segfault. After using the advices from some members the code worked but now its giving a segfault again for a different set of data. I cant understand why its working for one set and not for the other so i m reposting the code in case some of you can see other mistakes in it.

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//slots creation
	 bool nodeinscs,nodedestinrcs;
	 int counter=0;
    list<nodes> nodelist;
    list<nodes>::iterator tempnode,e,u; 
    list<int> reccolset;
	list<int> sendcolset;
	list<int>::iterator s,m,b;
	list<int> recnodes;
	list<int> sendnodes;
	d=0;
	for(i=0;i<n;i++){
                     nodelist.push_front(nodearr[i]);
                     }
     do {  
           cout<<"newslot"<<endl;
           reccolset.clear();
           sendcolset.clear();
           sendnodes.clear();
           recnodes.clear();    
		   for(e=nodelist.begin();e!=nodelist.end();++e){//nodelist loop
			   nodeinscs=false;
			   nodedestinrcs=false;
              for(b=sendcolset.begin();b!=sendcolset.end();++b){
				if(e->nid==*b)
				        {
							nodeinscs=true;  	
						  }
				}
				for(m=reccolset.begin();m!=reccolset.end();++m){
					if(e->dest==*m)
					    {
							nodedestinrcs=true;
						}
					}
					if(e->recv==0&&nodeinscs!=true&&nodedestinrcs!=true)
	                                  {
										  counter++;
										  cout<<"counter++"<<endl;				
										  sendnodes.push_front(e->nid);
										  recnodes.push_front(e->dest);
										  for(u=nodelist.begin();u!=nodelist.end();u++)
                                          {
                                                     if(u->nid==e->dest){
														  cout<<"GGG"<<endl;
                                                          u->recv--;
                                                          }
                                          }                                                 
										  //filling collison sets:
                                          sendcolset.push_back(e->dest);
										  s=e->neighbors.begin();
										  for(i=0;i<e->non;i++)
										                {
															cout<<"LLLL"<<endl;
											  reccolset.push_back(*s);
											  s++;
										                 }
											m=nodearr[e->dest].neighbors.begin();        
											for(i=0;i<=nodearr[e->dest].non;i++)
											          {			 
													 sendcolset.push_back(*m);
													 cout<<"HHH"<<endl;
													 m++;
												 }
												e=nodelist.erase(e);
										  }
							}
							cout<<"nodes for send  "<<endl;
					        for(s=sendnodes.begin();s!=sendnodes.end();++s){
		                    cout<<"  "<<*s;
								  }
					        cout<<"nodes for receive  "<<endl;
					        for(s=recnodes.begin();s!=recnodes.end();++s){
					     	cout<<"  "<<*s;
						    		      }			
					}while (counter<n);
					//edo tha ginei print to slot
					  
cout<<"ssssssssss";
 wait(10000);
}
Maybe you can post a complete, compilable example, as well as the input that it is segfaulting with.

Dave
You didn't take the earlier advice to refactor this into smaller functions. This is a very large function with insane indentation. Of course it will have problems.

My advice: break it up into smaller chunks. The problem will be easier to track down.

Try keeping functions to no more than 20 lines and no more than 3 levels of indentation. What this does is reduce the Cyclomatic Complexity of a function. http://en.wikipedia.org/wiki/Cyclomatic_complexity

It would also help to use a cleaner coding style. Style and legibility really does matter.
Ok. I took PanGalactic's advice and for the past two days i've been trying to divide my code into smaller functions. It has actually been really helpful since it is a lot easier to debug it now. The next problem i have to deal with is the following: I need to make a function in order to retrieve the data for the algorithm. I this function i'm declaring an array of type "nodes" which is a structure of integer values. This array is necessary for some other functions in my code but when i try to compile the program i get errors saying the array was not declared or something like that. Is it possible to do this? If so can you give me any advice on how to?
Topic archived. No new replies allowed.