【问题标题】:segmentation fault in ns2ns2 中的分段错误
【发布时间】:2013-03-31 05:17:56
【问题描述】:

我正在开发 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;
         }

        } 
       }

【问题讨论】:

  • 您可以发布您在 AODV 代码中所做的更改吗?在 cmets 中提及它。
  • 你能只显示引发段错误的确切行吗?
  • @dbasic 在 AODV 类中,我添加了 struct nb_traffic_stat *nblist;..n 我所做的其他更改......已经提到过......我的主要问题是当我运行模拟 4- 5 个节点我得到了正确的结果并且没有错误,但是当我增加 no say 9 时,我得到分段错误..当我增加模拟 tym 时也会发生同样的情况..这是由于内存问题而发生的事情吗??
  • @Jueecy 这是得到的错误:程序收到信号 SIGSEGV,分段错误。 nb_traffic_update(int, nb_traffic_stat**, int, int, int) () 中的 0x00000000006d390b 但此函数有时工作得很好,但在增加节点数或模拟时间时会引发错误..
  • @GarimaGoyal:在nb_traffic_update中,当nblist(不是*nblist)为NULL时是否存在任何条件。

标签: c++ networking ns2


【解决方案1】:

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

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

【讨论】:

  • @dbasic 是的,我已在 AODV 构造函数中将 nblist 初始化为 null。当调用 nb_traffic_update 时,我正在发送 nblist 的地址(我试图创建一个向此发送数据的邻居的链接列表节点)..
  • @Jueccy 我已经检查了 nblist==NULL..但仍然收到错误!
  • @Jueccy 我添加了 printf("%x",nblist);检查 nblist 是否为空......打印了一些地址......
【解决方案2】:

使用 gdb 运行 tcl,它将显示导致段错误的函数...

例如,gdb -args ns path/to/tcl/script.tcl

【讨论】:

  • 谢谢...你节省了我很多时间
猜你喜欢
  • 2018-03-21
  • 1970-01-01
  • 2015-04-29
  • 2014-06-19
  • 1970-01-01
  • 1970-01-01
  • 2016-03-19
  • 2014-01-22
  • 2019-09-04
相关资源
最近更新 更多