LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-16-2009, 01:53 PM   #1
fs11
Member
 
Registered: Aug 2006
Posts: 79

Rep: Reputation: 15
iterator initilization...segmentation fault


Hello every one,


I wrote a code for HMM model that I am building, and am having a problem with this function.The code compiles fine but when I run it, there is a segmentation fault. The segmentation fault occurs in the red coded area.

Now the reason is that BLUE loop is running more than the structure of the graph in the nested loop. Hence, after the first iteration(when the graph is iterated) I get a segmentation fault.Please note that I want to iterate the same graph again and again.

To counter this, I initialized the iterator for the graph coded in Yellow.But it seems that the iterator is not initialized and I am having the same segmentation fault. Any help would be highly appreciable.



Code:
Mat<double> HMM::reverse_partition_function(sequences* s, unsigned int line){

  unsigned int seq_len=int(s->seq[line].size());
  Mat<double> Z(tiny, int(states.size()), int(seq_len));//in log-space
  graph_type graph;
  graph_type graph1;
  int  new_state;
  int  old_state;
  //building the graph with required nodes and edges

  std::cout << "start of the loop 1" << endl;

graph1=build_graph();
graph=inverted(graph1);



std::cout << "start of the loop 2" << endl;

for(unsigned int k=0;k<seq_len;k++){

std::cout << "start of the loop fuck" << endl;

edge_list_type::const_iterator cur_edge;
graph_type::const_iterator cur_node;

unsigned int j=seq_len-1-k;
    if(j!=0){

for (graph_type::const_iterator cur_node = graph.begin() ,end_node = graph.end() ;cur_node != end_node; ++cur_node){
// cur_node->first will give the new_state

std::cout << "start of the loop 3" << endl;

//      for(unsigned int s_new=0;s_new<states.size();s_new++){
        vector<double> summands;


for (edge_list_type::const_iterator cur_edge = cur_node->second.begin(), end_edge = cur_node->second.end(); cur_edge != end_edge; ++cur_edge){
// *cur_edge will give the old_state
//      for(unsigned int s_old=0;s_old<states.size();s_old++){

 std::cout << "start of the loop 4" << endl;


          for(int l=0;l<=max_len;l++){

               double prior=log(prior_p[cur_node->first][*cur_edge]);//going backwards through model
              // double prior=0;
               if(j+l<seq_len){
               cout << "Start of loop 5"<< endl;
               cout << "value in new state"<< cur_node->first <<endl;
               cout << "value in old state"<< *cur_edge <<endl;
              // double sc=0;
               double sc=states[cur_node->first]->score(s, line, j, l);
               summands.push_back(prior+sc+(Z[*cur_edge][j+l]));
            //   summands.push_back(0);

                }

         }
     }

 for(int l=1;l<=1;l++){//end state only allows transition into background
          if(j+l==seq_len){
            if(end_tr[cur_node->first]>0){
              double prior=log(end_tr[cur_node->first]);
              double sc=states[cur_node->first]->score(s, line, j, l);
              summands.push_back(sc+prior);
            }
          }
        }

        if(summands.size()>0){
          Z[cur_node->first][j]=add_in_log_space(summands);
        }
        else{
          Z[cur_node->first][j]=tiny;
        }

}
    }

  else{

      for(unsigned int s_new=0;s_new<states.size();s_new++){
        if(start_tr[s_new]>0){
          double start_prior=log(start_tr[s_new]);
          vector<double> summands;
          for(unsigned int s_old=0;s_old<states.size();s_old++){
            for(int l=0;l<=max_len;l++){
              if(connectivity[s_new][s_old]>0){
                double prior=log(prior_p[s_new][s_old]);

                if(j+l<seq_len){
                  double sc=states[s_new]->score(s, line, j, l);
                  summands.push_back(start_prior+prior+sc+(Z[s_old][j+l]));
                }
                if(j+l==seq_len){
                  if(end_tr[s_new]>0){
                    double prior=log(end_tr[s_new]);
                    double sc=states[s_new]->score(s, line, j, l);
                    summands.push_back(sc+prior+start_prior);
                  }
                }
              }
            }
          }
          if(summands.size()>0){
            Z[s_new][j]=add_in_log_space(summands);
          }
          else{
            Z[s_new][j]=tiny;
          }
        }
      }
    }


  }
  return Z;
}


I am using this for building the graph.

Code:
using namespace std;
typedef std::vector<int> edge_list_type;
typedef std::map<int, edge_list_type> graph_type;

graph_type HMM::build_graph(){

graph_type graph;


// Enter the nodes of the graph

graph.insert(std::make_pair(1, std::vector<int>()));
graph.insert(std::make_pair(2, std::vector<int>()));
graph.insert(std::make_pair(3, std::vector<int>()));
graph.insert(std::make_pair(4, std::vector<int>()));


// Enter the edges of the graph
// Note that the edges are directed
// graph[Edge_from].push_back(Edge_to);



graph[1].push_back(1);
graph[1].push_back(2);
graph[2].push_back(4);
graph[3].push_back(1);
graph[4].push_back(3);






return graph;
}
 
Old 07-16-2009, 03:33 PM   #2
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
Aren't you iterating past the end of your container.
Code:
for (graph_type::const_iterator cur_node = graph.begin() ,end_node = graph.end() ;cur_node != end_node; ++cur_node)
. . .
. . .
for (edge_list_type::const_iterator cur_edge = cur_node->second.begin(), end_edge = cur_node->second.end(); cur_edge != end_edge; ++cur_edge)
I think you should be using
Code:
cur_node++
cur_edge++
 
Old 07-16-2009, 04:31 PM   #3
fs11
Member
 
Registered: Aug 2006
Posts: 79

Original Poster
Rep: Reputation: 15
Iterating one time is not a problem....It is the iteration of the graph the second time that is not being done and causing a segmentation fault...
i.e. The reinitialization of the iterator is not taking place
Help would be appreciable guys....thanks...
 
Old 07-16-2009, 04:55 PM   #4
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
ntubski is right in post #5, I was confused. I'm removing this to avoid confusing anyone else.

Last edited by norobro; 07-16-2009 at 10:11 PM.
 
Old 07-16-2009, 09:17 PM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
The segmentation fault occurs in the red coded area.
Where in that area (use a debugger)? Where does max_len come from?

Quote:
To counter this, I initialized the iterator for the graph coded in Yellow.
I don't see how the code in yellow could possibly help anything. The iterators you declare there will be shadowed by the ones you declare in the for loops. Also, please don't use yellow, it's really hard to read.

@noboro: I think you are a bit confused, the increment occurs after the loop (before going back to the test) regardless of whether ++cur_node or cur_node++ is used. post-increment and pre-increment do the exact same thing if that is the only expression in a statement (assuming the programmer didn't do something stupid with operator overloading).
 
Old 07-17-2009, 08:18 AM   #6
fs11
Member
 
Registered: Aug 2006
Posts: 79

Original Poster
Rep: Reputation: 15
I got the error resolved. The problem was not with the iterators but with one of the function that I was calling...Thanks anyways...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Segmentation fault mcamember Programming 7 12-18-2006 03:27 PM
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika SUSE / openSUSE 2 12-02-2005 09:34 AM
Segmentation fault alnreddy Linux - Software 1 11-05-2005 08:54 PM
Segmentation fault.. please help me! rpmadness Linux - Software 4 11-25-2003 06:47 PM
Segmentation fault suriyamohan Linux - General 5 10-21-2003 01:37 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration