segmentation fault

I am getting a segmentation fault during the execution of the following code. The program executes the first "for" loop correctly only on the first pass and it crashes while on the second pass (i think). Can anyone help me??
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
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;
                                          nodelist.erase(e);               
                                          sendnodes.push_front(e->nid);
                                          recnodes.push_front(e->dest);
                                         for(u=nodelist.begin()u!=nodelist.end();u++)
                                          {
                                              cout<<"GGG"<<endl;
                                                     if(u->nid==e->dest){
                                                          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(*s);
                                                     cout<<"HHH"<<endl;
                                                     m++;
                                                 }
                                          }
                            }
                            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);
Last edited on
Repost using [code][/code] tags so I can read the indentation please.
Last edited on
Sorry about the "preedit" version without the code tags. If anyone needs the whole program i would be glad to post it. I really need help with this one guys....
This seems fishy. It at least deserves a comment as to what is going on. e is erased. What does e represent after the erase?

1
2
3
                                          nodelist.erase(e);               
                                          sendnodes.push_front(e->nid);
                                          recnodes.push_front(e->dest);


Is nodelist a std::list? If so, e is invalid after the erase() and should not be dereferenced. This is a problem not only immediately, but later when attempting to increment e in the for() statement.

That seems right...Nodelist is an std::list. But what can i do to solve the problem?
Why not just do the push_backs before you do the erase?
I don't think thats the problem since the first couts after the erase come out fine. After all i'm not actually erasing e but the object on the list that e is pointing at. Right?
Last edited on
Take jsmith's advice. Also, erase() returns a value. Use it. You will need it to continue the loop properly.

e = nodelist.erase(e);

You will need to check whether e is at nodelist.end() at this point before continuing the loop.

The code is a bit of a mess. Consider refactoring the code so that no function is longer than about 20 lines.
Worked like a charm? Thanks a lot guys...
Topic archived. No new replies allowed.