如何解决此分段错误:以下代码的 11 错误?
Posted
技术标签:
【中文标题】如何解决此分段错误:以下代码的 11 错误?【英文标题】:How do I solve this segmentation fault : 11 error for the following code? 【发布时间】:2017-12-07 08:49:56 【问题描述】:这是我尝试使用本地搜索解决 2-SAT 问题的算法。但是,我似乎遇到了分段错误: 11. 问题从for ( int i =0; i<upper_limit; i++)
之后的主代码开始。
如何解决此错误?
#include <fstream>
#include <iostream>
#include<math.h>
#include <map>
#include <vector>
#include <set>
#include <random>
#include <math.h>
using namespace std;
bool check_clause(short &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<int> & elem)
short logic=0;
//Creating logic flag to construct teach clause Eg. x or !x2 = (10)(bin)= (2)(decimal)
for(int j =1; j>=0;j--)
if (elem[j]>0)
logic+=pow(2,1-j);
return logic;
vector <int> check_assignment(map <int,bool> &assignment,map <int, vector<int> > &data)
int count=0;
vector <int> 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 <int,vector<int> > & dictionary)
if (dictionary.find(number)==dictionary.end())
return false;
else
return true;
int main()
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> dis(0,1);
map <int, vector<int> > data;
map<int,vector<int> >::iterator it;
vector<int> row(2,0);
ifstream file_read("2sat1.txt");
int a;
int n;
file_read>>n;
cout<<n<<endl;
map <int, vector<int> > positives;
map <int, vector<int> > negatives;
int count=0;
while (file_read>>a)
row[0]=a;
file_read>>a;
row[1]=a;
data[count].insert(data[count].end(),row.begin(),row.end());
for (auto element: row)
if (element<0)
negatives[ -element ].push_back(count);
else
positives[ element ].push_back(count);
count++;
int n_rows=positives.size();
int incl_count=0;
vector <int> deletionList;
set <int> inclset;
// cout<<isKeyInMap(200001,positives)<<endl;
for (int i =1; i<=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<<"Inclusion set size: "<<inclset.size()<<endl;
int incl_size=inclset.size();
set <int> deletionSet(deletionList.begin(),deletionList.end());
cout<<"Set Size: "<<deletionSet.size()<<endl;
// sort(deletionList.begin(),deletionList.end());
for (auto i=deletionSet.rbegin(); i!=deletionSet.rend();++i)
for (auto item: data[*i])
cout<<item<<" ";
it=data.find(*i);
if (it!=data.end())
data.erase(it);
else
cout<<"Not found!"<<endl;
cout<<data.size()<<endl;
map <int,bool> 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<upper_limit; i++)
for (auto elem: inclset)
assignment[elem]= (bool) dis(gen);
for (auto elem: assignment)
cout<<elem.first<<":"<<elem.second<<endl;
cout<<"Size of assignment: "<<assignment.size()<<endl;
for (int j=0; j<local_iter_size; j++)
cout<<"Okay "<<j<<endl;
vector <int> unsatisfied;
unsatisfied = check_assignment(assignment,data);
if (unsatisfied.empty())
cout<<"Yay, we win!";
return 0;
int pick_random= unsatisfied[rand()% unsatisfied.size()];
vector <int> 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<<"Number of unfulfilled clauses: "<<unsatisfied.size()<<endl;
【问题讨论】:
您是否尝试过从代码中剥离内容直到段错误消失? 谢谢。我会试试看。 【参考方案1】:if (answer==false)
unsatisfied.push_back(count);
count+=1;
在 check_clause 函数中发现的这个子句有错误。我们应该推送不满足子句的键索引,即unsatisfied.push_back(elem.first)
。由于这个错误,程序尝试访问向量unsatisfied
范围之外的索引。
现在,代码运行良好。
【讨论】:
以上是关于如何解决此分段错误:以下代码的 11 错误?的主要内容,如果未能解决你的问题,请参考以下文章