【问题标题】:Getting segmentation fault in loop在循环中获取分段错误
【发布时间】:2015-11-26 12:45:07
【问题描述】:

我对这段代码有疑问:

while(true){
    BCNP(CBCV[CBCV.size()-1]);
    CBCV.pop_back();
    printf("BCN : %f\n", BCN());
    for(int q = 0; q < numNode; q++){
        CurrentNode = ((MobileNode*)Node::get_node_by_address(q));
        if(CurrentNode->ConditionNeighbor == BCN()){
            printf("currentnode : %f\n", CurrentNode->ConditionNeighbor);
            tcl.evalf("$ns_ at %f \"$node_(%d) color red\"",NOW,CurrentNode->nodeid());
            tcl.evalf("$ns_ at %f \"$node_(%d) label \\\"CH\\\"\"",NOW,CurrentNode->nodeid());
            break;
        }
    }
    CurrentNode->isCluster = true;
    for(int q = 0; q < CurrentNode->NodeQueue; q++){
        neighbor = ((MobileNode*)Node::get_node_by_address(CurrentNode->neib[q]));
        //if(neighbor->isCluster == false){
            neighbor->chFlag = true;
            CurrentNode->chFlag = true;
            neighbor->cluster = CurrentNode->nodeid();
            //printf("chFlag True : %d\n", neighbor->nodeid());
        //}
    }
    notRouted_ = 0;
    for(int q = 0; q < numNode; q++){
        CurrentNode = ((MobileNode*)Node::get_node_by_address(q));
        if(CurrentNode->chFlag == false){
            notRouted_++;
            //printf("CurrentNode : %d\n",CurrentNode->nodeid());
        }
    }
}

它给了我一个分段错误,但我不知道我在哪里遗漏了什么...

似乎没有定义一个变量,并且在循环中它将使用该变量,这就是我收到此错误的原因...
我昨晚根本没睡,现在我的大脑正在关闭,我真的需要帮助。

如果有人能告诉我这个错误的想法,那将是最好的。

    void            BCNP(double bcneighb){ BCN_ = bcneighb; };<br/>
    inline double   BCN(){ return BCN_; };
    inline int32_t  notRouted(){ return notRouted_; };
    int32_t numNode;
    std::vector<nsaddr_t> clusters;
    std::vector<double> CBCV; // __Could Be Cluster Vector
    double BCN_; // __Biggest Condition Neighbor
    int32_t notRouted_; // __number of nodes that have no cluster
    // CurrentNode is a class
    bool chFlag; // in CurrentNode
    double ConditionNeighbor; // CurrentNode
    numNode = 80;
    notRouted_ = 0;
    BCN_ = 0;

【问题讨论】:

  • 如果你不知道崩溃发生在哪里,是时候学习如何使用调试器了。在调试器中运行您的程序将捕获崩溃,允许您检查函数调用堆栈,并将调用堆栈向上移动到您的代码(如果调试器停止在例如库函数中)。当您到达您的代码时,您可以检查变量的值以帮助您了解崩溃发生的原因。至少,您应该尝试使用调试器来查明崩溃的位置,并告诉我们它在哪里。
  • 您应该在调试器中运行您的代码并逐行逐行查找分段错误的来源。
  • 感谢您快速回复..调试是无法完成的,因为我使用的是网络模拟器 2(NS2),您不能只在 NS2 中调试 c++ 代码,因为它是开源软件,我们只是在使用它,它有很多包含,我只是尝试调试它甚至运行它,但它不是这样运行的程序......我们使用它的方式是这样的:我们创建我们的协议和场景,然后我们使用 ./ns ./scenario.tcl 运行场景

标签: c++ loops segmentation-fault


【解决方案1】:

在更改 CurrentNodeneighbor 之后添加对 nullptr 的检查

喜欢:

CurrentNode = ((MobileNode*)Node::get_node_by_address(q));
if (CurrentNode == nullptr)
{
    cout << "Real bad version 1..." << endl;
}
else
{
    // Your normal code...
    if(CurrentNode->ConditionNeighbor == BCN()){
        printf("currentnode : %f\n", CurrentNode->ConditionNeighbor);
        tcl.evalf("$ns_ at %f \"$node_(%d) color red\"",NOW,CurrentNode->nodeid());
        tcl.evalf("$ns_ at %f \"$node_(%d) label \\\"CH\\\"\"",NOW,CurrentNode->nodeid());
        break;
    }
}

那么你可能会发现错误是对 nullptr 的取消引用。

记得在指针改变时添加类似的代码。

【讨论】:

  • 它说“错误:‘nullptr’没有在这个范围内声明”
  • 嗯...试试std::nullptr或只是null
  • 抱歉回复晚了,我上班睡着了,好累,不过我要说这个,这是有人能给我的最好答案。谢谢
猜你喜欢
  • 1970-01-01
  • 2013-06-17
  • 1970-01-01
  • 1970-01-01
  • 2017-08-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-27
  • 1970-01-01
相关资源
最近更新 更多