使用向量作为类成员的分段错误 [英] segmentation fault in using vectors as members of class
问题描述
这是我的代码:
#include< iostream>
#include< vector>
#include < fstream>
#include< stdio.h>
#include< string>
#include< cstring>
使用命名空间std;
class gate
{
private:
string op;
vector< string> ip;
字符串类型;
public:
//接受门的类型
void takein_type(string a)
{type = a;}
//接受门的输入
void ips(string c)
{ip.push_back(c);}
//获取门的输出
void input_op(string a)
{op = a;}
//读取门输入
vector< string> read_ip()
{return ip;}
//读取门输出值
string read_op()< br $>
{return op;}
//读门类型
字符串read_type()
{return type;}
//删除最后一个输入实际上它是下一个门的输出
void rm()
{ip.erase(ip.begin()+ ip.size() - 1);}
int fanout(gate g1)
{
int fout = 0;
int p = g1.ip.size() - 1;
while(p> = 0)
{if(g1.ip [p] == op)
{fout = fout + 1;}
p = p-1;
}
返回fout;
}
string ret_type()
{返回类型;}
int fanin(g1门)
{
int fin = 0;
for(int vv = 0; vv< g1.ip.size(); vv ++)
{
if(g1.ip [ vv] == op)
{fin ++;}
}
返回fin;
}
无效输出()
{
cout<<" output:"<<< op<< endl;
cout<<" gate type:"<<类型<< endl;
for(int i = 0; I< ip.size(); i ++)
{cout<<" inputs:"<<< ip [i]<< endl;}
}
void flush()
{ip.clear();}
};
int main()
{
vector< gate> GATE;
vector< int> inp;
vector< int> out;
vector< string> inp_str;
vector< string> out_str;
char * pch;
string str2,str_temp,line;
int pins = 0,ss = 0, rr = 0,c = 0,bit = 0;
gate g;
ifstream obj(" b15_C.txt");
if(obj.is_open())
{
while(!obj.eof())
{
getline(obj,line);
char * temp;温度=&安培;线[0];
if(* temp!=''#'')
{
pch = strtok(temp ,&,,()");
while(pch!= NULL)
{
if(ss == 1)
{inp_str.push_back(pch); ss = 0;}
if(rr == 1)
{out_str.push_back(pch); rr = 0;}
if(pch [0] ==''我'&& pch [1 ] == '' N '' &安培;&安培; PCH [2] == '' P '' &安培;&安培; PCH [3] == '' U '' &安培;&安培; PCH [4] == '' T'')
{
inp.push_back(pins);
pins ++;
ss = 1;
}
else if(pch [0] == '' O '' &安培;&安培; PCH [1] == '' U '' &安培;&安培; PCH [2] == '' T '' &安培;&安培; PCH [3] == '' P ''&& pch [4] ==''U''&& pch [5] ==''T'')
{
out.push_back(销);
pins ++;
rr = 1;
}
else if(pch [ 0] ==''='')
{
if(c> 0)
{
g.rm();
GATE.push_back(g); g.flush();
}
g.input_op(str_temp); bit = 0;
c ++;
}
else if(str_temp ==" =")
{g.takein_type(pch);}
else if(bit == 1)
{g.ips(pch); }
else if(str_temp ==" nand" || str_temp ==" and" || str_temp ==" or||| str_temp == " nor" || str_temp ==" not" || str_temp ==" buf" || str_temp ==" exor" || str_temp ==" exnor")
{ g.ips(pch); bit = 1;}
str2 = str_temp;
str_temp = pch;
pch = strtok(NULL,",()");
}
}
>
}
obj.close();
}
else {cout<< 无法打开文件; }
GATE.push_back(g);
//每种类型的门都没有
gate g1,g2;
int nand = 0,exorg = 0,norg = 0,exnorg = 0,andg = 0,notg = 0,org = 0,buf = 0,nand_fin = 0,nand_fout = 0,and_fin = 0,and_fout = 0,not_fin = 0,not_fout = 0,or_fout = 0,or_fin = 0,nor_fin = 0,nor_fout = 0,exor_fin = 0,exor_fout = 0, exnor_fin = 0,exnor_fout = 0;
int qq = 0;
for(; qq< GATE.size(); qq ++)
{
if(GATE [qq] .read_type()==" nand")
{nand = nand + 1;}
if(GATE [qq] .read_type()==" and)
{andg = andg + 1;}
if (GATE [qq] .read_type()=="或)
{org = org + 1;}
if(GATE [qq] .read_type() ==" not")
{notg = notg + 1;}
if(GATE [qq] .read_type()==" buf")
{buf = buf + 1;}
if(GATE [qq] .read_type()==" nor")
{norg = norg + 1;}
if(GATE [qq] .read_type()==" e xor")
{exorg = exorg + 1;}
if(GATE [qq] .read_type()==" exnor")
{exnorg = exnorg + 1;}
}
//查找粉丝
int f = 0;
vector< int> FanOut;
int u,v;
for(u = 0; u< GATE.size(); u ++)
{
for(v = 0; v< GATE.size(); v ++)
{
f = f + GATE [u] .fanout(GATE [v]);
}
FanOut.push_back(f);
f = 0;
}
//主要输入粉丝
int aa,bb,cc,dd = 0;
>
vector< string> sa;
for(aa = 0; aa< inp_str.size(); aa ++)
{
for(bb = 0; bb< GATE.size(); bb ++)
{
sa = GATE [bb] .read_ip();
for (cc = 0; cc< sa.size(); cc ++)
{
if(inp_str [aa] == sa [cc])
{dd = dd + 1;}
}
}
FanOut.push_back(dd);
dd = 0;
}
//主要输出粉丝
for(int ee = 0; ee< out.size(); ee ++)
{
FanOut.push_back(1);
}
gate tt;
int and_d = 0,or_d = 0,nand_d = 0,nor_d = 0,exor_d = 0, exnor_d = 0,buf_d = 0,not_d = 0;
int and_r = 0,or_r = 0,nand_r = 0,nor_r = 0,exor_r = 0,exnor_r = 0,buf_r = 0,not_r = 0;
int nand_D = 0,nand_R = 0;
int ii = 0,jj = 0;
for(ii = 0; ii< GATE.size(); ii ++)
{
for(jj = 0; jj< GATE。 size(); jj ++)
{
//为nand
if(GATE [ii] .ret_type()==" nand")
{
if(GATE [jj] .ret_type ()==" nand")
{
if(GATE [ii] .fanout(GATE [jj])> 0)
{nand_d = nand_d + 1;}
if(GATE [ii] .fanin(GATE [jj])> 0)
{nand_r = nand_r + 1;}
}
}
/ *
// for和
if(tt.ret_type()==" and")
{
if(ss) .ret_type()=="和")
{
and_d = and_d + ss.fanout(tt)> 0?1:0;
and_r = and_r + ss.fanin(tt)> 0?1:0;
}
}
// for或
if(tt.ret_type()=="或)
{
if(ss.ret_type ()=="或)
{
or_d = or_d + ss.fanout(tt)> 0?1:0;
or_r = or_r + ss.fanin(tt)> 0?1:0;
}
}
// for nor
if(tt.ret_type()= =" nor")
{
if(ss.ret_type()==" nor")
{
nor_d = nor_d + ss.fanout(tt)> 0?1:0;
nor_r = nor_r + ss.fanin(tt)> 0?1:0;
}
}
// for exor
if(tt.ret_type()==" ; exor)
{
if(ss.ret_type()==" exor")
{
exor_d = exor_d + ss.fanout(tt)> 0?1:0;
exor_r = exor_r + ss.fanin(tt)> 0?1:0;
}
}
// for exnor
if(tt.ret_type()==" exnor" ;)
{
if(ss.ret_type()==" exnor")
{
exnor_d = exnor_d + ss.fanout(tt)> 0?1:0;
exnor_r = exnor_r + ss.fanin(tt)> 0?1:0;
}
}
// for buf
if(tt.ret_type()==" buf")
{
if(ss.ret_type()==" buf")
{
buf_d = buf_d + ss.fanout(tt) > 0?1:0;
buf_r = buf_r + ss.fanin(tt)> 0?1:0;
}
}
// for not
if(tt.ret_type()==" not")
{
if(ss.ret_type()==" not")
{
not_d = not_d + ss.fanout(tt)> 0?1: 0;
not_r = not_r + ss.fanout(tt)> 0?1:0;
}
}
* /
}
if(nand_d> 0){nand_D = nand_D + 1;}
if(nand_r> 0 ){nand_R = nand_R + 1;}
nand_d = 0; nand_r = 0;
}
//最大扇出
int b = 0;
int z = 0;
for(b = 0; b< FanOut.size(); b ++)
{
if(z< FanOut [b])
{z = FanOut [b];}
}
cout<< ; z<< endl;
// F + 1行
int nfs [z + 1];
for(int kk = 0; kk< = z; kk ++)
{nfs [kk] = 0;}
for(int hh = 0; hh< = z; hh ++)
{
for(int b = 0; b< FanOut.size(); b ++)
{
if(hh == FanOut [b])
{nfs [hh] = nfs [hh] +1; } $ / $
}
for(int w = 0; w< = z; w ++)
{cout<< nfs [w]<< endl;}
cout<< inp.size()<<"" " ;;
for(int ww = 0; ww< inp.size(); ww ++)
{cout<< inp [ww]<<" ; " ;;}
cout<< endl;
cout<<< out.size()<<"" " ;;
for(int x = 0; x< out.size(); x ++)
{cout<< out [x]<<" ; " ;;}
cout<< endl;
if(nand!= 0)
{ cout<<" nand"<< nand<<" "<< nand_D<<" "<< nand_R<<" "<< endl;}
if(andg!= 0)
{cout<<" and"<<以及g<<" "<< and_d<<" "<< and_r<<" "<< endl;}
if(notg!= 0)
{cout<<" not"<< notg<<" "<< not_d<<" "<< not_r<<" "<< endl;}
if(org!= 0)
{cout<<" or"<<组织LT&;<" "<< or_d<<" "<< or_r<<" "<< endl;}
if(norg!= 0)
{cout<<" nor"<< NORG<<" "<< nor_d<<" "<< nor_r<<" "<< endl;}
if(exorg!= 0)
{cout<<"" exor"<< exorg<<" "<< exor_d<<" "<< exor_r<<" "<< endl;}
if(exnorg!= 0)
{cout<<"" exnor"<< exnorg<<" "<< exnor_d<<" "<< exnor_r<<" "<< endl;}
if(buf!= 0)
{cout<<"" buf"<< BUF<<" "<< buf_d<<" "<< buf_r<<" "<< endl;}
返回0;
}
当我运行gdb时我收到以下消息
程序收到信号SIGSEGV,分段错误。
0xb7ec6375 in std :: basic_string< char, std :: char_traits< char> ;,std :: allocator< char> > ::〜basic_string()来自/usr/lib/libstdc++.so.6
这个没有给我任何行号来调试,即使我输入''bt full ''我得到同样的东西,
i是STL的新手但知道C ++(amatuerish).....
请帮助我理解这里的问题,
Here is my code:
#include <iostream>
#include <vector>
#include <fstream>
#include <stdio.h>
#include <string>
#include <cstring>
using namespace std;
class gate
{
private:
string op;
vector<string> ip;
string type;
public:
//to take in the type of gate
void takein_type(string a)
{type=a;}
//to take in input of gate
void ips(string c)
{ip.push_back(c);}
//to take in output of gate
void input_op(string a)
{op=a;}
//to read gate inputs
vector<string> read_ip()
{return ip;}
//to read value of output of gate
string read_op()
{return op;}
//read type of gate
string read_type()
{return type;}
//erase last input added as actually it is output of next gate
void rm()
{ip.erase(ip.begin()+ip.size()-1);}
int fanout(gate g1)
{
int fout=0;
int p=g1.ip.size()-1;
while(p>=0)
{ if(g1.ip[p]==op)
{fout=fout+1;}
p=p-1;
}
return fout;
}
string ret_type()
{return type;}
int fanin(gate g1)
{
int fin=0;
for(int vv=0; vv<g1.ip.size(); vv++)
{
if(g1.ip[vv]==op)
{fin++;}
}
return fin;
}
void output()
{
cout<<"output: "<<op<<endl;
cout<<"gate type: "<<type<<endl;
for(int i=0; i<ip.size(); i++)
{cout<<"inputs: "<<ip[i]<<endl;}
}
void flush()
{ip.clear();}
};
int main()
{
vector<gate> GATE;
vector<int> inp;
vector<int> out;
vector<string> inp_str;
vector<string> out_str;
char* pch;
string str2, str_temp, line;
int pins=0, ss=0, rr=0, c=0, bit=0;
gate g;
ifstream obj("b15_C.txt");
if (obj.is_open())
{
while (! obj.eof() )
{
getline(obj,line);
char* temp; temp=&line[0];
if(*temp!=''#'')
{
pch = strtok(temp," ,()");
while (pch != NULL)
{
if(ss==1)
{inp_str.push_back(pch);ss=0;}
if(rr==1)
{out_str.push_back(pch);rr=0;}
if(pch[0]==''I''&&pch[1]==''N''&&pch[2]==''P''&&pch[3]==''U''&&pch[4]==''T'')
{
inp.push_back(pins);
pins++;
ss=1;
}
else if(pch[0]==''O''&&pch[1]==''U''&&pch[2]==''T''&&pch[3]==''P''&&pch[4]==''U''&&pch[5]==''T'')
{
out.push_back(pins);
pins++;
rr=1;
}
else if(pch[0]==''='')
{
if(c>0)
{
g.rm();
GATE.push_back(g);g.flush();
}
g.input_op(str_temp);bit=0;
c++;
}
else if(str_temp=="=")
{g.takein_type(pch);}
else if(bit==1)
{g.ips(pch);}
else if(str_temp=="nand" || str_temp=="and" || str_temp=="or" || str_temp=="nor" || str_temp=="not" || str_temp=="buf" || str_temp=="exor" || str_temp=="exnor")
{g.ips(pch);bit=1;}
str2=str_temp;
str_temp=pch;
pch = strtok (NULL, " ,()");
}
}
}
obj.close();
}
else {cout << "Unable to open file"; }
GATE.push_back(g);
//no of gates of each type
gate g1,g2;
int nand=0, exorg=0, norg=0, exnorg=0, andg=0, notg=0, org=0, buf=0, nand_fin=0, nand_fout=0, and_fin=0, and_fout=0, not_fin=0, not_fout=0, or_fout=0, or_fin=0, nor_fin=0, nor_fout=0, exor_fin=0, exor_fout=0, exnor_fin=0, exnor_fout=0;
int qq=0;
for(;qq<GATE.size();qq++)
{
if(GATE[qq].read_type()=="nand")
{nand=nand+1;}
if(GATE[qq].read_type()=="and")
{andg=andg+1;}
if(GATE[qq].read_type()=="or")
{org=org+1;}
if(GATE[qq].read_type()=="not")
{notg=notg+1;}
if(GATE[qq].read_type()=="buf")
{buf=buf+1;}
if(GATE[qq].read_type()=="nor")
{norg=norg+1;}
if(GATE[qq].read_type()=="exor")
{exorg=exorg+1;}
if(GATE[qq].read_type()=="exnor")
{exnorg=exnorg+1;}
}
//finding fanouts
int f=0;
vector<int> FanOut;
int u,v;
for(u=0; u<GATE.size(); u++)
{
for(v=0; v<GATE.size(); v++)
{
f=f+GATE[u].fanout(GATE[v]);
}
FanOut.push_back(f);
f=0;
}
//primary input fanouts
int aa,bb,cc,dd=0;
vector<string> sa;
for(aa=0; aa<inp_str.size(); aa++)
{
for(bb=0; bb<GATE.size(); bb++)
{
sa=GATE[bb].read_ip();
for(cc=0; cc<sa.size(); cc++)
{
if(inp_str[aa]==sa[cc])
{dd=dd+1;}
}
}
FanOut.push_back(dd);
dd=0;
}
//primary output fanouts
for(int ee=0; ee<out.size(); ee++)
{
FanOut.push_back(1);
}
gate tt;
int and_d=0, or_d=0, nand_d=0, nor_d=0, exor_d=0, exnor_d=0, buf_d=0, not_d=0;
int and_r=0, or_r=0, nand_r=0, nor_r=0, exor_r=0, exnor_r=0, buf_r=0, not_r=0;
int nand_D=0, nand_R=0;
int ii=0,jj=0;
for(ii=0; ii<GATE.size(); ii++)
{
for(jj=0; jj<GATE.size(); jj++)
{
//for nand
if(GATE[ii].ret_type()=="nand")
{
if(GATE[jj].ret_type()=="nand")
{
if(GATE[ii].fanout(GATE[jj])>0)
{nand_d=nand_d+1;}
if(GATE[ii].fanin(GATE[jj])>0)
{nand_r=nand_r+1;}
}
}
/*
//for and
if(tt.ret_type()=="and")
{
if(ss.ret_type()=="and")
{
and_d=and_d+ss.fanout(tt)>0?1:0;
and_r=and_r+ss.fanin(tt)>0?1:0;
}
}
//for or
if(tt.ret_type()=="or")
{
if(ss.ret_type()=="or")
{
or_d=or_d+ss.fanout(tt)>0?1:0;
or_r=or_r+ss.fanin(tt)>0?1:0;
}
}
//for nor
if(tt.ret_type()=="nor")
{
if(ss.ret_type()=="nor")
{
nor_d=nor_d+ss.fanout(tt)>0?1:0;
nor_r=nor_r+ss.fanin(tt)>0?1:0;
}
}
//for exor
if(tt.ret_type()=="exor")
{
if(ss.ret_type()=="exor")
{
exor_d=exor_d+ss.fanout(tt)>0?1:0;
exor_r=exor_r+ss.fanin(tt)>0?1:0;
}
}
//for exnor
if(tt.ret_type()=="exnor")
{
if(ss.ret_type()=="exnor")
{
exnor_d=exnor_d+ss.fanout(tt)>0?1:0;
exnor_r=exnor_r+ss.fanin(tt)>0?1:0;
}
}
//for buf
if(tt.ret_type()=="buf")
{
if(ss.ret_type()=="buf")
{
buf_d=buf_d+ss.fanout(tt)>0?1:0;
buf_r=buf_r+ss.fanin(tt)>0?1:0;
}
}
//for not
if(tt.ret_type()=="not")
{
if(ss.ret_type()=="not")
{
not_d=not_d+ss.fanout(tt)>0?1:0;
not_r=not_r+ss.fanout(tt)>0?1:0;
}
}
*/
}
if(nand_d>0){nand_D=nand_D+1;}
if(nand_r>0){nand_R=nand_R+1;}
nand_d=0; nand_r=0;
}
//max fanout
int b=0;
int z=0;
for(b=0; b<FanOut.size(); b++)
{
if(z<FanOut[b])
{z=FanOut[b];}
}
cout<<z<<endl;
//F+1 lines
int nfs[z+1];
for(int kk=0; kk<=z; kk++)
{nfs[kk]=0;}
for(int hh=0; hh<=z; hh++)
{
for(int b=0; b<FanOut.size(); b++)
{
if(hh==FanOut[b])
{nfs[hh]=nfs[hh]+1;}
}
}
for(int w=0; w<=z; w++)
{cout<<nfs[w]<<endl;}
cout<<inp.size()<<" ";
for(int ww=0; ww<inp.size(); ww++)
{cout<<inp[ww]<<" ";}
cout<<endl;
cout<<out.size()<<" ";
for(int x=0; x<out.size(); x++)
{cout<<out[x]<<" ";}
cout<<endl;
if(nand!=0)
{cout<<"nand "<<nand<<" "<<nand_D<<" "<<nand_R<<" "<<endl;}
if(andg!=0)
{cout<<"and "<<andg<<" "<<and_d<<" "<<and_r<<" "<<endl;}
if(notg!=0)
{cout<<"not "<<notg<<" "<<not_d<<" "<<not_r<<" "<<endl;}
if(org!=0)
{cout<<"or "<<org<<" "<<or_d<<" "<<or_r<<" "<<endl;}
if(norg!=0)
{cout<<"nor "<<norg<<" "<<nor_d<<" "<<nor_r<<" "<<endl;}
if(exorg!=0)
{cout<<"exor "<<exorg<<" "<<exor_d<<" "<<exor_r<<" "<<endl;}
if(exnorg!=0)
{cout<<"exnor "<<exnorg<<" "<<exnor_d<<" "<<exnor_r<<" "<<endl;}
if(buf!=0)
{cout<<"buf "<<buf<<" "<<buf_d<<" "<<buf_r<<" "<<endl;}
return 0;
}
when i run gdb i am getting the following message
Program received signal SIGSEGV, Segmentation fault.
0xb7ec6375 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string () from /usr/lib/libstdc++.so.6
thisdoes not give me any line number to debug, even when i type in ''bt full'' i get the same thing,
i am new to STL but know C++ (amatuerish).....
kindly help me understand whats the issue here,
推荐答案
我认为字符串存储存在一些问题,我所拥有的字符串向量可能会有一些问题,但是我无法弄清楚是什么?
文件b15_C.txt很大,因此需要将很多字符串作为push_back传递给向量。这是否意味着某种内存溢出?我可以增加内存模型的大小吗?
当我输入一个较小的文件程序运行时没有任何分段错误,所以这一点表明有责任归咎于内存溢出
i think there is some problem with string storage, the string vectors that I have may be have some problem, but i am not able to make out what?
the file b15_C.txt is huge, so many strings needed to be push_back to the vector. does this mean there is a memory overflow of some sort ? can i increase the memory model size ?
also when i input a smaller file the program runs without any segmentation fault, so this point indicates strong reason to blame memory overflow
同样当我在Visual C ++中运行它时,我得到Debug assertion failed在行:else if(bit == 1)
The same piece of code when i run it in Visual C++ i get "Debug assertion failed" on the line: else if(bit==1)
strtok()有问题吗?另外我在VC ++中收到警告,说使用strtok_s()而不是
is there some problem with the strtok() ? also i get a warning in VC++ saying use strtok_s() instead
这篇关于使用向量作为类成员的分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!