Error message saying Runtime Error (SEG) keeps coming up - c++

I submitted my solution for a problem( http://opc.iarcs.org.in/index.php/problems/WORDLIST ) to the site .It is served by an online judge.Every time i submit it, it says runtime error and shows the following message :
Runtime Error: SEG
Description: Segmentation fault
Runtime: 0
What is this fault and how do i fix it?
Note:My knowledge of c++ is intermediate and i would be grateful if you could explain it in a simple way
This is my code for the problem:
#include<iostream>
#include<ctype.h>
#include<string.h>
using namespace std;
class problem
{
public:
int nol,k,j,w,cp;
char inp[1000][80],qed[1000][80];
problem()
{
k=j=w=cp=0;
}
void input()
{
cin>>nol;cin.get();
for(int i=0;i<nol;i+=1)
{
cin.getline(inp[i],80);
cin.clear();
}
cout<<endl;
lineprocess();
}
void lineprocess()
{
if(k<nol)
wordprocess();
else
display();
}
void wordprocess()
{
for(;;)
{
if(ch(inp[k][cp]))
{qed[w][j]=0;break;}
else
{
qed[w][j]=tolower(inp[k][cp]);
j+=1;cp+=1;
}
}
decide();
}
void decide()
{
if(inp[k][cp]==0)
{
w+=1;
cp=0;
j=0;
k+=1;
lineprocess();
}
else
{
w+=1;
j=0;
cp+=1;
wordprocess();
}
}
int ch(char a)
{
if(!isalpha(a))
return 1;
else return 0;
}
void display()
{
char dumm[80]="";
qed[w][0]='\0';
sortarray();
removerep();
for(int i=0;i<=w;i+=1)
if(strcmp(qed[i],dumm)!=0)
if(strcmp(qed[i],qed[i+1])!=0)
cout<<qed[i]<<endl;
}
void sortarray()
{
char ub[80];
for(;checksort();)
{
for(int q=0;q<w;q++)
{
if(strcmp(qed[q],qed[q+1])>0)
{
strcpy(ub,qed[q]);
strcpy(qed[q],qed[q+1]);
strcpy(qed[q+1],ub);
}
}
}
}
int checksort()
{
int t=0;
for(int i=0;i<w;i+=1)
{
if(strcmp(qed[i],qed[i+1])>0)
{t+=1;break;}
}
return t;
}
void removerep()
{
int nodw=0;
for(int i=0;i<w;i+=1)
if(strcmp(qed[i],qed[i+1])!=0)
nodw+=1;
cout<<nodw<<endl;
}
};
int main()
{
problem r2d2;
r2d2.input();
return 0;
}

From the linked page:
You may assume that N ≤ 10000
Since you are using
char inp[1000][80],qed[1000][80];
you are likely accessing memory beyond the valid range, which will cause you problems. Change them to:
char inp[10000][80],qed[10000][80];

Related

How can I use variables in C++ without passing into a function

Is there a way in C where you can use variables that you don't pass in variables?
I'm working on some code that someone else started on and I'm just modifying it.
Here is some code that I have.
#include <iostream>
using namespace std;
void rotate90() {
for (int w=0;w<n;w++) {
for (int h=0;h<n;h++) {
chart2[h][w]=chart[n1-w][n1-h];
}
}
for (int h=0;h<n;h++) {
for (int w=0;w<n;w++) {
chart[h][w]=chart2[h][w];
}
}
}
int main() {
......
else
{
rotate90;
}
}
}

"Error: void value not ignored as it ought to be" - Understanding the error and trying to overcome it

Following is my code snippet.The error occurs in the display_results() method.
class results
{
public:
bool majority;
int major_element = 0;
void show_results()
{
if (majority)
{
cout<<"Majority found "<<major_element;
}
else
cout<<"No majority element found";
}
};
void display_results(results* p, int number_of_testcases)
{
for(int i = 0; i<number_of_testcases;i++)
{
//Error occurs in next line
*p[i].show_results();
}
}
Can anyone explain me why is this happening?

how to efficiently appraoch for SPOJ Square free numbers?

I tried my best to solve Spoj problem No Squares numbers but I'm getting (TLE). Please tell how to approach. I'm unable to find any proper approach. Here is my code:
#include <bits/stdc++.h>
using namespace std;
#define size 10000004
int mark[size+1];
void sieve()
{
for(int i=2,k;(k=i*i)<=size;++i)
{
for(int j=k;j<=size;j=j+k)
mark[j]=-1;
}
}
int fn(int a,int b,int c)
{
int i,j,k,cnt=0;
for(i=a;i<=b;++i)
{
j=i;
if(mark[j]!=-1)
{
while(j>0)
{
if(j%10==c)
{
cnt+=1;
break;
}
else
j=j/10;
}
}
}
return cnt;
}
int main()
{
sieve();
int t,a,b,c;
cin>>t;
for(int i=0;i<t;t++)
{
cin>>a>>b>>c;
cout<<fn(a,b,c)<<endl;
}
}

segmentation fault in ns2

m working on ns2...did some changes in aodv.cc and added some functions of my own
void nb_traffic_update(int id,struct nb_traffic_stat **nblist,int nid,int flag,int hop_count)
..to detect sinkhole attack..when m running the code with small no of nodes i get results but when i increase the number of nodes i get segmentation fault.
This is my nb_traffic.h file
struct nb_traffic_stat
{
int id;
int recvrequest;
int routereply;
int no_of_hops;
//int no_of_updation;
struct nb_traffic_stat *next;
};
struct traffic_stat
{
int id;
struct nb_traffic_stat **list;
struct traffic_stat *next;
};
struct ftraffic_stat
{
int sendrequest;
int routereply;
};
modification to aodv.cc
struct traffic_stat *tlist=NULL,*ttail=NULL;
void
AODV::recvReply(Packet *p) {
...
if (ih->daddr() == index) { // If I am the original source
.....
nb_traffic_update(index,&nblist,ih->saddr(),1,rp->rp_hop_count);//1 is for receiving the route reply
}
}
void
AODV::recvRequest(Packet *p) {
....
/*after ensuring this is the new routerequest*/
struct hdr_cmn *ch = HDR_CMN(p);
if(ch->num_forwards()==1)
{
nb_traffic_update(index,&nblist,rq->rq_src,0,0);//0 is for receiving the request
}
}
my neighbour traffic update function
void nb_traffic_update(int id,struct nb_traffic_stat **nblist,int nid,int flag,int hop_count)
{
int n;
//printf("inside nb_traffic_update:%d\n",id);
if(*nblist==NULL)
{
struct nb_traffic_stat *ptr;
ptr=(struct nb_traffic_stat*)malloc(sizeof(struct nb_traffic_stat));
ptr->id=nid;
ptr->next=NULL;
if(flag==0)
{
ptr->recvrequest=1;
ptr->routereply=0;
ptr->no_of_hops=0;
//ptr->no_of_updation=0;
}
else
{
ptr->recvrequest=0;
ptr->routereply=1;
ptr->no_of_hops=hop_count;
//ptr->no_of_updation=1;
}
*nblist=ptr;
struct traffic_stat *sptr;
sptr=tlist;
while(sptr!=NULL&&sptr->id!=id)
{
sptr=sptr->next;
}
assert(sptr!=NULL);
sptr->list=nblist;
}
else
{
int found=0;
struct nb_traffic_stat *tptr,*prevtptr;
tptr=*nblist;
while(tptr!=NULL&&tptr->id<=nid)
{
if(tptr->id==nid)
{
found=1;
break;
}
prevtptr=tptr;
tptr=tptr->next;
}
if(found)
{
if(flag==0)
{
tptr->recvrequest++;
}
else
{
tptr->routereply++;
tptr->no_of_hops=hop_count;
//tptr->no_of_updation++;
}
}
else
{
struct nb_traffic_stat *ptr;
ptr=(struct nb_traffic_stat*)malloc(sizeof(struct nb_traffic_stat));
ptr->id=nid;
if(flag==0)
{
ptr->recvrequest=1;
ptr->routereply=0;
ptr->no_of_hops=0;
//ptr->no_of_updation=0;
}
else
{
ptr->recvrequest=0;
ptr->routereply=1;
ptr->no_of_hops=hop_count;
//ptr->no_of_updation=1;
}
ptr->next=prevtptr->next;
prevtptr->next=ptr;
}
}
}
You are not checking for nblist to be NULL inside the nb_traffic_update(int, nb_traffic_stat**, int, int, int) function, which leads to the segfault.
Also inside the conditional statement if (*nblist==NULL) you are doing: *nblist=ptr;. Which means *NULL = ptr; which may cause the segfault.
Run your tcl with gdb it will show the function which cause the segment fault...
for ex, gdb -args ns path/to/tcl/script.tcl

Help About a Error

HI,
I got this Error in my code i can't understand how to pass a command line argument while the exe of my programe is not created how i write the name of that .exe file.
C:\Program Files\Microsoft Visual Studio\MyProjects\filehandling\file.cpp(205) : error C2451: conditional expression of type 'class std::basic_fstream >' is illegal
Ambiguous user-defined-conversion
#include "iostream"
#include "cstdlib"
#include "cstdio"
#include "ctime"
#include "fstream"
#include "istream"
using namespace std;
class shapes
{
public:
virtual void draw()=0;
virtual void save(fstream &out)=0;
virtual void open(fstream &in)=0;
};
class myline : public shapes
{
private:
int sx,sy,ex,ey,color;
public:
myline()
{}
myline(int x1, int y1, int x2, int y2, int clr)
{
sx=x1;
sy=y1;
ex=x2;
ey=y2;
color=clr;
}
void draw()
{
cout<<"Line-draw()"<<endl;
}
void save(fstream &out)
{
out<<"L"<<"\n";
out<<sx<<""<<sy<<""<<ex<<""<<ey<<""<<color<<"\n";
}
void open(fstream &in)
{
in>>sx>>sy>>ex>>ey>>color;
}
};
class myrectangle: public shapes
{
private:
int sx,sy,ex,ey,color;
public:
myrectangle()
{}
myrectangle(int x1, int y1,int x2, int y2,int clr)
{
sx=x1;
sy=y1;
ex=x2;
ey=y2;
color=clr;
}
void draw()
{
cout<<"Rectangle-draw()"<<endl;
}
void save(fstream &out)
{
out<<"R"<<"\n";
out<<sx<<""<<sy<<""<<ex<<""<<ey<<""<<color<<"\n";
}
void open(fstream &in)
{
in>>sx>>sy>>ex>>ey>>color;
}
};
class mycircle: public shapes
{
private:
int sx, sy, radius, color;
public:
mycircle()
{
}
mycircle(int x1, int y1, int r, int clr)
{
sx=x1;
sy=y1;
radius=r;
color=clr;
}
void draw()
{
cout<<"Circle-draw()"<<endl;
}
void save(fstream &out)
{
out<<"C"<<"\n";
out<<sx<<""<<sy<<""<<radius<<""<<color<<"\n";
}
void open(fstream &in)
{
in>>sx>>sy>>radius>>color;
}
};
struct node
{
void*obj;
node*link;
};
class objarray
{
private:
node*head;
public:
objarray()
{
head= NULL;
}
void add(void*o)
{
node*temp = new node;
temp->obj=o;
temp->link=NULL;
if(head==NULL)
head=temp;
else
{
node*q;
q=head;
while(q->link != NULL)
q=q->link;
q->link=temp;
}
}
void*getobj(int i)
{
node*q;
q=head;
int n;
for (n=1; n<i; n++)
{
q=q->link;
}
return(q->link);
}
int getcount()
{
int n=0;
node*q;
q=head;
while(q != NULL)
{
q=q->link;
n++;
}
return n;
}
~objarray()
{
node *q;
q=head;
while(q != NULL)
{
head = head->link;
delete q;
q=head;
}
}
};
int main(int argc ,char*argv[])
{
fstream file;
char choice;
int clmum,sx,sy,ex,ey,rad;
shapes*ptr;
objarray arr;
char a[2];
int i;
if(argc==2)
file.open(argv[1], ios::in|ios::out);
while(file)
{
file>>a;
if(strcmp(a,"L")==0)
{
myline*l = new myline();
l->open(file);
arr.add(l);
}
if(strcmp(a,"R")==0)
{
myrectangle *a=new myrectangle();
a->open(file);
arr.add(a);
}
if(strcmp(a,"C")==0)
{
mycircle*c=new mycircle();
c->open(file);
arr.add(c);
}
}
int count = arr.getcount();
for(i=1; i<=count; i++)
{
ptr=(shapes*)arr.getobj(i);
ptr->draw();
}
srand((unsigned ) time(NULL));
while(1)
{
cout<<endl<<"1.Line 2. Rectanle 3.Circle 4.Exit"<<endl;
cout<<"Your Choice:";
fflush(stdin);
cin.get(choice);;
clmum=rand()%16;
sx=rand()%638;
sy=rand()%478;
ex=rand()%638;
ey=rand()%478;
rad=rand()%200;
myline*l;
myrectangle*a;
mycircle*c;
switch(choice)
{
case '1':
l = new myline(sx, sy, ex,ey,clmum);
if(l=NULL)
exit(1);
arr.add(l);
cout<<"Following Line added to array"<<endl;
cout<<"sx="<<sx<<"sy="<<sy<<"ex ="<<ex<<"ey ="<<ey<<"color ="<<clmum<<endl;
break;
case '2':
a = new myrectangle(sx,sy,ex,ey,clmum);
if(a==NULL)
exit(1);
arr.add(a);
cout<<"Following Rectangle added to array"<<endl;
cout<<"sx="<<sx<<"sy="<<sy<<"ex ="<<ex<<"ey ="<<ey<<"color ="<<clmum<<endl;
break;
case '3':
c=new mycircle(sx,sy,rad,clmum);
if(c==NULL);
exit(1);
arr.add(c);
cout<<"Following Circle added to array"<<endl;
cout<<"sx="<<sx<<"sy="<<sy<<"rad ="<<rad<<"color"<<clmum<<endl;
break;
case '4':
if(argc==1)
{
cout<<"Enter File name:";
char name[67];
cin>>name;
file.open(name,ios::out);
}
count=arr.getcount();
file.seekp(0L,ios::beg);
file.clear();
for(i=1; i<=count;i++)
{
ptr=(shapes*) arr.getobj(i);
ptr->save(file);
}
file.close();
cout<<"Array save to file......exiting"<<endl;
exit(1);
}
}
return 0;
}
Here's your problem area (at least the area of the problem you've identified):
while(file)
{
file>>a;
What you're getting should be a warning, not an error -- there's one conversion that should be used here. Even though what it's told you is technically wrong, it's still done you a favor by identifying buggy code. The problem is that you're testing whether the read succeeded before you actually do the read. Therefore, when/if the read fails, you'll execute another iteration of your loop before the loop exits.
You want to combine the reading and testing so you'll detect a failed read immediately after it happens. You can do this by replacing both of the lines above with: while (file >> a) {
Your problem is the line 205, while (file). It should be while (!file.eof())
As pointed out by CariElf, your file won't turn to NULL or 0 or something like that during that while loop. On the other hand, the file's "end of file" marker will be true at some point.
About your... well, other question I guess ("i can't understand how to pass a command line argument while the exe of my programe is not created"):
You can tell VS you want to pass some command line arguments by changing "Command Arguments" in Project->Properties, on the Configuration Properties->Debugging tab.