473,772 Members | 2,510 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

segmentation fault in using vectors as members of class

10 New Member
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(str ing 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.be gin()+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<<en dl;
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,lin e);

char* temp; temp=&line[0];

if(*temp!='#')

{
pch = strtok(temp," ,()");

while (pch != NULL)
{

if(ss==1)
{inp_str.push_b ack(pch);ss=0;}
if(rr==1)
{out_str.push_b ack(pch);rr=0;}


if(pch[0]=='I'&&pch[1]=='N'&&pch[2]=='P'&&pch[3]=='U'&&pch[4]=='T')
{
inp.push_back(p ins);
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(p ins);
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=="n and" || str_temp=="and" || str_temp=="or" || str_temp=="nor" || str_temp=="not" || str_temp=="buf" || str_temp=="exor " || str_temp=="exno r")
{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.si ze();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_bac k(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_bac k(dd);
dd=0;
}


//primary output fanouts


for(int ee=0; ee<out.size(); ee++)
{
FanOut.push_bac k(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()=="n and")
{
if(GATE[jj].ret_type()=="n and")
{

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.fa nout(tt)>0?1:0;
or_r=or_r+ss.fa nin(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+s s.fanout(tt)>0? 1:0;
exor_r=exor_r+s s.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){na nd_D=nand_D+1;}
if(nand_r>0){na nd_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_stri ng<char, std::char_trait s<char>, std::allocator< char> >::~basic_strin g () 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,
Oct 11 '09
10 4165
newb16
687 Contributor
There is a problem that you don't check for capitalized strings like 'AND' and then something in your tokenizing goes wrong. If you capitalize 'AND' in you smaller example, you can reproduce the crash.
Oct 12 '09 #11
sagitalk
10 New Member
thank you for spotting the bug,
one more problem due to the capitalized words was that a vector element was being deleted while the vector was actually empty!!

so i added adequate if statements to take care of the capitalized words and put a statement to check whether the vector is not empty before deleting anything

problem rectified!!

thank you!!
Oct 15 '09 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

3
5210
by: mblome | last post by:
Hi everybody! I came across a very strange problem with stl vectors during developement of a mesh creation program. As the program is quite large I can only post small parts of it. Basically I have a std::vector<SPoly> xtop; where SPoly simply is: struct SPoly { int start, end;
5
3326
by: saratoga | last post by:
Hi, all! I've 'Segmentation fault' in this code. I've found that there are two calls to destructor. TestSTL.h: #include <list> #include <string> #include <iostream> using namespace std;
5
2998
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I understood that a segmentation fault can occur whenever I declare a pointer and I leave it un-initialized. So I thought the problem here is with the (const char *)s in the stuct flightData (please note that I get the same fault declaring as char * the...
27
3367
by: Paminu | last post by:
I have a wierd problem. In my main function I print "test" as the first thing. But if I run the call to node_alloc AFTER the printf call I get a segmentation fault and test is not printed! #include <stdlib.h> #include <stdio.h> typedef struct _node_t {
8
1625
by: ChristophK | last post by:
Hallo, this is my first post and I hope I'm doing well, but if not, please let me know. My problem: First all classes and a main-file (I simplified everything to get to the core of the problem): /*---------------- start of code ---------------------------*/
1
5386
by: cesco | last post by:
Hi, I'm using the boost library program_options for parsing the command line given to my program as described in the class Parameter: // Main.cpp #include "Parameter.h" using namespace std; int main(int ac, char* av)
8
14701
by: Bryan | last post by:
Hello all. I'm fairly new to c++. I've written several programs using std::vectors, and they've always worked just fine. Until today. The following is a snippet of my code (sorry, can't include all of it- it's over 1k lines long). In addition, I'm including an "include" file where structures like "stack" are defined. Again, it's really long. I doubt the problem lies there, though, because the include file is used in many other...
2
3311
by: Steve | last post by:
I have segmentation fault when calling resize on an stl vector. I'm unsure if I'm doing something horribly wrong or if the stl is being dumb. The code breaks down like this: ------------------------------------- class Device { public: Device() { m_isBusy = false;
25
3389
by: jbholman | last post by:
I am pretty new to C and doing my first project in C. I actually read almost the entire FAQ, but can't seem to figure out this problem. I have a structure. I have a list of these structures. Inside each structure, I have two members: a list of strings, and a string. I have made a sample program below that exhibits the error I am having. I also read about Valgrind, and used it to tell me where I was getting the segmentation fault,...
0
10264
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10039
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9914
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8937
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7461
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6716
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5355
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.