gpt4 book ai didi

c++ - ns2 中的段错误

转载 作者:行者123 更新时间:2023-11-28 03:20:01 25 4
gpt4 key购买 nike

我在 ns2 上工作...在 aodv.cc 中做了一些更改并添加了我自己的一些功能

void nb_traffic_update(int id,struct nb_traffic_stat **nblist,int nid,int flag,int hop_count)

..检测天坑攻击..当我用少量节点运行代码时,我得到了结果,但是当我增加节点数时,我得到了段错误。这是我的 nb_traffic.h 文件

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

对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
}
}

我的邻居路况更新函数

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

}
}

最佳答案

您没有在 nb_traffic_update(int, nb_traffic_stat**, int, int, int) 函数中检查 nblist 是否为 NULL,这会导致段错误。

同样在条件语句 if (*nblist==NULL) 中,您正在执行:*nblist=ptr;。这意味着 *NULL = ptr; 可能会导致段错误。

关于c++ - ns2 中的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15726589/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com