title: How do I solve this segmentation fault : 11 error for the following code?
body: <p>This is an algorithm in which I am trying to solve the 2-SAT problem using local search. However, I seem to be getting a segmentation fault : 11. The problem starts from the main code after <code>for ( int i =0; i&lt;upper_limit; i++)</code>.
  <br><br>
  <strong>How can I fix this error?</strong></p>
  
  <pre><code>#include &lt;fstream&gt;
  #include &lt;iostream&gt;
  #include&lt;math.h&gt;
  #include &lt;map&gt;
  #include &lt;vector&gt;
  #include &lt;set&gt;
  #include &lt;random&gt;
  #include &lt;math.h&gt;
  
  using namespace std;
  
  bool check_clause(short &amp;logic,bool x1,bool x2)
  {
      switch(logic)
          {
              case 0:
                  return ( (not x1) or (not x2) ); 
              case 1:
                  return ( (not x1) or x2 );
                  break;
              case 2:
                  return ( x1 or (not x2) );
                  break;
              case 3:
                  return( x1 or x2 );
                  break;
          }
          return false;
  }
  
  int check_logic(vector&lt;int&gt; &amp; elem)
  {
      short logic=0;
      //Creating logic flag to construct teach clause Eg. x or !x2 = (10)(bin)= (2)(decimal)
      for(int j =1; j&gt;=0;j--)
      {
          if (elem[j]&gt;0)
          {
              logic+=pow(2,1-j);
          }
      }
      return logic;
  }
  
  vector &lt;int&gt; check_assignment(map &lt;int,bool&gt; &amp;assignment,map &lt;int, vector&lt;int&gt; &gt; &amp;data)
  {   int count=0;
      vector &lt;int&gt; unsatisfied;
      for (auto elem: data)
      {   //Variables for both vertex indices for clause and the logic flag
          int first_vertex=abs(elem.second[0]);
          int second_vertex=abs(elem.second[1]);
          short logic=check_logic(elem.second);
  
          //Retrieving randomly generated boolean variables for first and second vertex of current clause
          bool x1 = assignment[first_vertex];
          bool x2 = assignment[second_vertex];
          bool answer=check_clause(logic,x1,x2);
  
          if (answer==false)
          {
              unsatisfied.push_back(count);
          }
          count+=1;
  
      }
  
      return unsatisfied;
  }
  
  bool isKeyInMap(int number, map &lt;int,vector&lt;int&gt; &gt; &amp; dictionary)
  {
      if (dictionary.find(number)==dictionary.end())
      {
          return false;
      }
      else
      {
          return true;
      }
  }
  
  int main()
  {
      random_device rd;
      mt19937 gen(rd());
      uniform_int_distribution&lt;&gt; dis(0,1);
      map &lt;int, vector&lt;int&gt; &gt; data;
      map&lt;int,vector&lt;int&gt; &gt;::iterator it;
      vector&lt;int&gt; row(2,0);   
      ifstream file_read("2sat1.txt");
      int a;
      int n;
      file_read&gt;&gt;n;
      cout&lt;&lt;n&lt;&lt;endl;
      map &lt;int, vector&lt;int&gt; &gt; positives;
      map &lt;int, vector&lt;int&gt; &gt; negatives;
      int count=0;
  
      while (file_read&gt;&gt;a)
      {
          row[0]=a;
          file_read&gt;&gt;a;
          row[1]=a;
          data[count].insert(data[count].end(),row.begin(),row.end());
  
  
          for (auto element: row)
          {
              if (element&lt;0)
                  {
                      negatives[ -element ].push_back(count);
                  }
              else 
              {
                  positives[ element ].push_back(count);
              }
  
  
          }
  
  
          count++;
      }
  
  
      int n_rows=positives.size();
      int incl_count=0;
  
      vector &lt;int&gt; deletionList;
      set &lt;int&gt; inclset;
      // cout&lt;&lt;isKeyInMap(200001,positives)&lt;&lt;endl;
  
      for (int i =1; i&lt;=n;i++)
      {
          bool pos_entry=isKeyInMap(i,positives);
          bool neg_entry=isKeyInMap(i,negatives);
  
          if ( pos_entry != neg_entry )
          {
              if (pos_entry==true)
              {
                  deletionList.insert(deletionList.end(),positives[i].begin(),positives[i].end());
              }
              else
              {
                  deletionList.insert(deletionList.end(),negatives[i].begin(),negatives[i].end());
              }
          }
          else if ((pos_entry==neg_entry) and (pos_entry==true))
          {
  
              inclset.insert(i);
          }
  
  
      }
      cout&lt;&lt;"Inclusion set size: "&lt;&lt;inclset.size()&lt;&lt;endl;
      int incl_size=inclset.size();
  
      set &lt;int&gt; deletionSet(deletionList.begin(),deletionList.end());
      cout&lt;&lt;"Set Size: "&lt;&lt;deletionSet.size()&lt;&lt;endl;
      // sort(deletionList.begin(),deletionList.end());
  
      for (auto i=deletionSet.rbegin(); i!=deletionSet.rend();++i)
      {
  
          for (auto item: data[*i])
          {
              cout&lt;&lt;item&lt;&lt;" ";
          }
          it=data.find(*i);
          if (it!=data.end())
          {
              data.erase(it);
          }
          else
          {
              cout&lt;&lt;"Not found!"&lt;&lt;endl;
          }
  
  
      }
      cout&lt;&lt;data.size()&lt;&lt;endl;
  
  
      map &lt;int,bool&gt; assignment;
      int upper_limit= (int) ceil ( log2(incl_size) );
      long local_iter_size= 2*pow(incl_size,2);
  
      for (auto elem: inclset)
      {
          assignment[elem]=(bool) dis(gen);
      }
  
  
  
  
      for ( int i =0; i&lt;upper_limit; i++)
      {
  
          for (auto elem: inclset)
          {
              assignment[elem]= (bool) dis(gen);
  
          }
  
          for (auto elem: assignment)
          {
              cout&lt;&lt;elem.first&lt;&lt;":"&lt;&lt;elem.second&lt;&lt;endl;
          }
          cout&lt;&lt;"Size of assignment: "&lt;&lt;assignment.size()&lt;&lt;endl;
  
          for (int j=0; j&lt;local_iter_size; j++)
          {
              cout&lt;&lt;"Okay "&lt;&lt;j&lt;&lt;endl;
              vector &lt;int&gt; unsatisfied;
              unsatisfied = check_assignment(assignment,data);
  
              if (unsatisfied.empty())
              {
                  cout&lt;&lt;"Yay, we win!";
                  return 0;
              }
  
              int pick_random= unsatisfied[rand()% unsatisfied.size()];
              vector &lt;int&gt; record= data[pick_random];
              short logic=check_logic(record);
              int first_vertex=abs(record[0]);
              int second_vertex=abs(record[1]);
              bool x1=assignment[first_vertex];
              bool x2= assignment[second_vertex];
  
              if (check_clause(logic,not x1,x2)==true)
              {
                  assignment[first_vertex]
                  = not x1;
              }
              else
              {
                  assignment[second_vertex]=not x2;
              }
              cout&lt;&lt;"Number of unfulfilled clauses: "&lt;&lt;unsatisfied.size()&lt;&lt;endl;
          }
  
  
      }
  
  
  }
  </code></pre>