segmentation fault in ns2 - c++

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

Related

Swap 2 values of a stack does not work

#include <iostream>
#include <string.h>
using namespace std;
#define NMAX 10 // pre-processing directive
template<typename T>
class Stack {
public:
T Smain, Saux;
T stackArray[NMAX];
int topLevel;
int getTopLevel()
{
return this->topLevel;
}
void push(T x)
{
if (topLevel >= NMAX - 1)
{
cout<<"The stack is full: we have already NMAX elements!\n";
return;
}
stackArray[++topLevel] = x;
}
int isEmpty()
{
return (topLevel < 0);
}
T pop()
{
if (isEmpty()) {
cout<<"The stack is empty! \n";
T x;
return x;
}
return stackArray[topLevel--];
}
T peek()
{
if (isEmpty())
{
cout<<"The stack is empty! \n";
T x;
return x;
}
return stackArray[topLevel];
}
void afficher() {
cout<<"On affiche la pile:";
for ( int i=0; i<=topLevel; i++ )
cout<<stackArray[i]<<" ";
cout<<endl;
}
Stack()
{
topLevel = -1;
}
~Stack() {
}
void change(int i)
{
while (Smain.topLevel>i){
Saux.push(Smain.pop());}
T aux1=Smain.pop();
T aux2=Smain.pop();
Smain.push(aux1);
Smain.push(aux2);
while (Saux.topLevel>=0){
Smain.push(Saux.pop());}
}
};
int main()
{
Stack<int> stack1;
Stack<int> stack2;
stack1.push(1);
stack1.push(2);
stack1.push(3);
stack1.push(4);
change(3);
stack1.afficher();
return 0;
}
This program has to swap the i and i-1 position of a stack, but i get the error: 'change' was not declared in this scope and i don't know how to make it work. please help me and thanks to you in advance.
change is declared inside the class Stack, so it becomes a method on a Stack instance.
You probably want to call it as stack1.change(3) instead.
You cannot call change() function which is inside a class directly.instead use an class object to call it.
class stack{
}objectname;
int main(){
objectname.change(3);
}

Error message saying Runtime Error (SEG) keeps coming up

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];

C++ code for inheritance breaks when trying to assign a value

I am trying to write this code where there are 2 classes inherited from a parent class.But this code breaks when I try to use it in a link list.Here is the code
class airship{
public:
airship(){};
// virtual ~ airship();
virtual void setData (string)=0;
virtual void showData()=0;
void setNext(airship* newptr);
airship* getNext();
void setType(int n){airshipType=n;}
int getType(){return airshipType;}
void setCount(int n){maxPassengerCount=n;};
int getCount(){return maxPassengerCount;}
void setWeight(int n){maxCargoWeight=n;}
int getWeight(){return maxCargoWeight;}
protected:
int maxPassengerCount;
int maxCargoWeight;
int airshipType;
private:
airship* next;
};
class airplane: public airship{
protected:
const char* airplaneName;
int engineType;
double range;
public:
airplane():airship(){};
// ~ airplane();
void setData(string);
void showData();
void setEngine(int n){engineType=n;}
int getEngine(){return engineType;}
void setRange(int n){range=n;}
int getRange();
};
class balloon : public airship{
protected:
const char* balloonName;
int gasType;
double maxAltitude;
public:
balloon():airship(){};
// ~balloon();
void setData(string);
void showData();
void setGas(int);
int getGas();
void setAlt(int);
int getAlt();
};
class mylist{
private:
airship* headAirship;
public:
void createlist(fstream&);
void setAirship(airship* Node){Node=headAirship;}
airship* getAirship(){return headAirship;}
};
void airship::setNext(airship* Node)
{
this->next=Node;
}
void airplane::setData(string line)
{
string line1;
bool flag=true;
int npos=0,nend,count=0;
while(line.c_str()!=NULL && flag)
{
nend=line.find(",",npos);
if (nend!=-1)
{
line1=line.substr(npos,nend);
}
else
{
line1=line;
flag=false;
}
line=line.substr(nend+1,line.length());
if (count==0)
this->airshipType=atoi(line1.c_str());
else if(count==1)
this->airplaneName=line1.c_str();
else if(count==2)
this->maxPassengerCount=atoi(line1.c_str());
else if(count==3)
this->maxCargoWeight=atoi(line1.c_str());
else if(count==4)
this->engineType=atoi(line1.c_str());
else
this->range=atoi(line1.c_str());
count=count+1;
}
}
void balloon::setData(string line)
{
string line1;
bool flag=true;
int npos=0,nend,count=0;
while(line.c_str()!=NULL && flag)
{
nend=line.find(",",npos);
if (nend!=-1)
{
line1=line.substr(npos,nend);
}
else
{
line1=line;
flag=false;
}
line=line.substr(nend+1,line.length());
if (count==0)
this->airshipType=atoi(line1.c_str());
else if(count==1)
this->balloonName=line1.c_str();
else if(count==2)
this->maxPassengerCount=atoi(line1.c_str());
else if(count==3)
this->maxCargoWeight=atoi(line1.c_str());
else if(count==4)
this->gasType=atoi(line1.c_str());
else
this->maxAltitude=atoi(line1.c_str());
count=count+1;
}
}
void mylist::createlist(fstream &myfile)
{
string ipdata;
int type;
while (getline(myfile,ipdata))
{
airship* newNode;
type=ipdata.find(",",0);
string ch=ipdata.substr(type-1,type);
if (atoi(ch.c_str())==0)
{
airplane* planeNode=new airplane();
planeNode->setData(ipdata);
newNode=planeNode;
}
else
{
balloon* balloonNode=new balloon();
balloonNode->setData(ipdata);
newNode=balloonNode;
}
newNode->setNext(headAirship);
this->headAirship=newNode;
}
}
int main()
{
mylist* list=0;
airship* firstNode=0,*otherNode=0;
/*if(argv[1]==NULL)
{
cout<<"File not Found"<<endl;
return -1;
}*/
fstream ipFile("data.txt",ios::in | ios::out);
if(ipFile.is_open())
{
list->createlist(ipFile);
}
list->setAirship(firstNode);
return 0;
}
The airplane::setdata(string) function does not work properly and it is not able to detect headAirship pointer in the createList function.And that is why I get a memory exception error in the createlist function when I try to do this:newNode->setNext(headAirship);
Unhandled exception at 0x00912ae0 in Assignment4Solution2.exe: 0xC0000005:
Access violation reading location 0x00000000.
I think the problem is here:
mylist* list=0;
In that line, you create a pointer to a myList object, but you don't make it point to any such object; it's pointing to address 0
Then you do
list->createlist(ipFile);
Since list is not pointing an address containing a myList object, invoking any of the myList methods is supposed to give the error you observed: Access violation reading location 0x00000000.
Instead, when you create the list pointer, invoke the constructor for myList so that the object gets created and the pointer is initialized to point to it
myList * list = new myList();
Hope that helps :)

Segmentation fault in this program

Shortly: I have two linear linked lists which represent a polynomial. I have to multiply them. I've wrote everything down here. The only problem is that I get a segmentation fault on one line (if(n.grad<r->a.grad) - also marked in the code below).
I've tried this program in Borland and it works!
In CodeBlocks or MinGW it simply crashes.
#include <iostream>
using namespace std;
struct poli
{
int grad;
float coe;
};
struct Nod
{
poli a;
Nod *adr;
};
Nod *v,*sf,*v1,*vs,*vp;
void add_first(Nod *&v, poli n)
{
if(v)
{
Nod *p;
p=new Nod;
p->a=n;
p->adr=v;
v=p;
}
else
{
v=new Nod;
sf=v;
v->a=n;
v->adr=0;
}
}
void add_last(Nod *&v, poli n)
{
if(!v)
{
v=new Nod;
v->a=n;
v->adr=0;
}
else
{
Nod *p,*sf;
sf=v;
while(sf->adr)
sf=sf->adr;
p=new Nod;
p->a=n;
p->adr=0;
sf->adr=p;
sf=p;
}
}
void add_before(Nod *v, int val, poli val_add)
{
Nod *p, *q;
if(v->a.grad==val)
{
p=new Nod;
p->a=val_add;
p->adr=v;
v=p;
}
else
{
p=v;
while(p->adr->a.grad!=val&&p->adr->adr)
p=p->adr;
if(p->adr->a.grad==val)
{
q=new Nod;
q->a=val_add;
q->adr=p->adr;
p->adr=q;
}
}
}
void produs(Nod *v, Nod *v1, Nod *&vp)
{
Nod *p,*q,*r;
int gasit;
poli n;
p=v;
while(p)
{
q=v1;
while(q)
{
n.grad=p->a.grad+q->a.grad;
n.coe=p->a.coe*q->a.coe;
r=vp;
gasit=0;
while(r)
{
if(n.grad==r->a.grad)
{
r->a.coe+=n.coe;
gasit=1;
}
r=r->adr;
}
if(!gasit)
{
r=vp;
if(n.grad<r->a.grad) /////////////// HERE I get the call stack
add_first(vp,n);
else
{
while(r->adr&&n.grad>r->adr->a.grad)
r=r->adr;
if(r->adr&&n.grad<r->adr->a.grad)
add_before(vp,r->adr->a.grad,n);
else
add_last(vp,n);
}
}
q=q->adr;
}
p=p->adr;
}
}
void tipar(Nod *v)
{
Nod *p;
p=v;
while(p)
{
cout<<"+"<<p->a.coe<<"x^"<<p->a.grad;
p=p->adr;
}
}
int main()
{
int n,m,i;
poli a;
cout<<"Cate elemente are polinomu' 1?";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Baga gradu'";
cin>>a.grad;
cout<<"Introdu-mi coe";
cin>>a.coe;
add_last(v,a);
add_last(vs,a);
}
cout<<"Cate elemente are polinomu' 2?";
cin>>m;
for(i=0;i<m;i++)
{
cout<<"Baga gradu'";
cin>>a.grad;
cout<<"Introdu-mi coe";
cin>>a.coe;
add_last(v1,a);
}
produs(v,v1,vp);
tipar(vp);
return 0;
}
Here is the call stack window contents:
#0 004016C4 produs (v=0x4d25a0, v1=0x4d26b8, vp=#0x474018) at F:\Programe\ma ballz(23.02) (F:\Programe\suma polinom\main.cpp:142)
#1 004019F2 main () at F:\Programe\ma ballz(23.02) (F:\Programe\suma polinom\main.cpp:195)
looks like vp is NULL, or garbage, and then you do r = vp...
Probably NULL because it didn't fall in the loop.
It has a garbage value because you never initialize it with a value. You just declare it in the beginning.
Anyway you should learn to give meaningful names to your variables so it will be more readable and therefor maintainable.

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.