Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何找出G++;优化C++;密码_C++_Linux - Fatal编程技术网

C++ 如何找出G++;优化C++;密码

C++ 如何找出G++;优化C++;密码,c++,linux,C++,Linux,今天,我遇到了一个奇怪的问题。我的C++代码可以在调试模式下工作。 使用g++-g编译代码。但是,当我使用g++-O优化代码时。它会卡在某个地方。似乎存在死循环。有人知道如何发现这种错误吗?当我用DDD调试程序调试代码时,它工作得很好。谢谢 代码部分(我发现卡住的部分来自此部分)粘贴在下面: void Solver::reduceRoutes() { int V=pinst->get_V(); //get the given number of vehicles if(

今天,我遇到了一个奇怪的问题。我的C++代码可以在调试模式下工作。 使用g++-g编译代码。但是,当我使用g++-O优化代码时。它会卡在某个地方。似乎存在死循环。有人知道如何发现这种错误吗?当我用DDD调试程序调试代码时,它工作得很好。谢谢

代码部分(我发现卡住的部分来自此部分)粘贴在下面:

void Solver::reduceRoutes()
{
    int V=pinst->get_V();   //get the given number of vehicles
    if(int(curSol_.size())<=V)    // return when solution has no extra routes 
        return;

    int routeNum1,routeNum2;   // the two routes modified

    listSize=int(0.2*pinst->get_N());

    short TheNode,anode;          // the second node
    float totalInc;               //the obj increase of a candidate position
    Route::iterator it;
    vector<short> candidateList;
    vector<short> validCandidateList;  //nodes can be moved to
    vector<float> totalImpList;
    int solSize=int(curSol_.size());

    while(solSize>V)
    {
        //  cout <<"debug6.0 ";
        routeNum1=psol->findRouteWithMinC(curSol_);
        cout <<" debug6.1 "<<curSol_.size()<<" "<<routeNum1;
        while(curSol_[routeNum1].size()>2)
        {
            it=curSol_[routeNum1].begin();
            it++;
            TheNode=*it;
            candidateList=pinst->get_PNL(TheNode,listSize);

            // evaluate the effect of moving the node to each possible position

            for(unsigned int i=1;i<candidateList.size();i++)      //the first node is itself
            {
                anode=candidateList[i];

                routeNum2=RouteNumList[anode];  //find the route of second node
                if(routeNum2!=routeNum1)         //inter route move
                {  
                    totalInc=evaluateAreduceRouteMove(curSol_,routeNum1,routeNum2,TheNode,anode);
                    totalImpList.push_back(totalInc);
                    validCandidateList.push_back(anode);
                }
            }

            //find the best position to insert the
            int ii=(min_element(totalImpList.begin(),totalImpList.end())-totalImpList.begin());
            anode=validCandidateList[ii];

            it=find(curSol_[routeNum1].begin(),curSol_[routeNum1].end(),TheNode);
            curSol_[routeNum1].erase(it);       //remove from route1

            routeNum2=RouteNumList[anode];
            it=find(curSol_[routeNum2].begin(),curSol_[routeNum2].end(),anode);
            ++it;
            curSol_[routeNum2].insert(it,TheNode);  //insert to the second route
            RouteNumList[TheNode]=routeNum2;        //update route number
            //improve the modified routes

            psol->doTwoOpt(curSol_[routeNum2]);
            totalImpList.clear();
            validCandidateList.clear();
        }

        //update route number list
        for(unsigned int i=routeNum1+1;i<curSol_.size();i++)
        {
            for(it=curSol_[i].begin();it!=curSol_[i].end();it++)
                RouteNumList[*it]-=1;

        }

        RouteNumList[0]=0;
        // eliminate the empty route
        curSol_.erase(curSol_.begin()+routeNum1);
        solSize=curSol_.size();
        cout <<" debug6.3 "<<solSize<< " \n";

    }

    return;
}
void解算器::reduceRoutes()
{
int V=pinst->get_V();//获取给定数量的车辆
if(int(curSol.size())get_N());
短节点,阳极;//第二个节点
float totalInc;//候选职位的目标增加
路由::迭代器;
病媒候选名单;
vector validCandidateList;//可以将节点移动到
向量极权主义者;
int solSize=int(游标大小());
while(solSize>V)
{

//cout为什么在循环开始时,您将solSize键入int
int solSize=int(curSol.size())
,但在while循环中却没有这样做
solSize=curSol.size();
。您可能希望调查调试和优化版本中的值


此外,代码中似乎有些地方有浮点数/双精度数与(可能是)整数进行比较。这可能是无限循环的原因。

为什么在循环开始时将solSize键入int
int solSize=int(curSol\uu.size())
但在while循环中没有这样做
solSize=curSol\uuuz.size();
。您可能需要调查调试和优化版本中的值


此外,代码中似乎有一些地方有浮点数/双精度数与(可能是)整数进行比较。这可能是无限循环的原因。

您如何知道这是挂起的地方?是否来自printf/cout调试?如果是这样,您的思路是正确的。在这种情况下,这可能是一个强大的工具


我同意Gangadhar,检查您的类型。如果您指定其他优化级别,它是否仍然挂起?如果您关闭优化并且不包含调试符号(no-O或-g选项),该怎么办?

你怎么知道你挂在这里?是从printf/cout调试来的吗?如果是这样,你走对了方向。在这种情况下,这可能是一个强大的工具


我同意Gangadhar,检查您的类型。如果您指定了其他优化级别,它是否仍然挂起?如果您关闭优化并且不包含调试符号(无-O或-g选项),该怎么办?

除非您遇到了编译器错误(并且我每十年遇到的错误不到一个),问题是您的代码没有完全定义的行为,并且在调试模式下以一种方式编译,在优化时以另一种方式编译。编写优化器的人通常不会对未定义或未指定的行为负责;如果代码错误,输出也会错

您是否已尝试启用所有可能的警告?其中一些警告可能会让您了解问题所在。快速查看您包含的代码对我没有任何建议


也有可能在程序的其他地方有未定义的行为,最有可能是堆损坏。弄清楚这一点总是很有趣的。

除非您遇到了编译器错误(我每十年遇到的错误不到一个),问题是您的代码没有完全定义的行为,并且在调试模式下以一种方式编译,在优化时以另一种方式编译。编写优化器的人通常不会对未定义或未指定的行为负责;如果代码错误,输出也会错

您是否已尝试启用所有可能的警告?其中一些警告可能会让您了解问题所在。快速查看您包含的代码对我没有任何建议


程序中的其他地方也可能存在未定义的行为,最有可能是堆损坏。弄清楚这一点总是很有趣的。

您可以使用对
pstack
的几个调用来找出程序在循环时的位置。如果这不可用或不能产生令人满意的结果,您最好的选择是ode>cout
s…在函数的每个点中都有大量的
cout
s来缩小它的挂起位置。没有这些信息,我们只是瞎猜。使用
-Wall
编译并确保没有任何警告。

您可以使用几个调用
pstack
来找出您的程序在哪里运行当它循环时。如果这不可用或不能产生令人满意的结果,你最好的选择是
cout
s…在函数的每个点中都有很多
cout
s,以精确缩小它的挂起位置。没有这些信息,我们只是瞎猜。使用
-Wall
编译并确保没有任何警告.

首先要做的是确保所有代码都编译干净。
大多数此类错误都会导致您(无意中)使用某些未定义的行为或意外地执行一些愚蠢的操作。编译器会警告您大多数此类情况,因此您可以利用这些情况

添加以下内容:

-ansi-学究式-W-墙壁式-Werror


这将迫使您修复所有简单的问题。

首先要做的是确保所有代码都编译干净。
大多数此类错误都会导致您(无意中)使用某些未定义的行为或意外地执行一些愚蠢的操作。编译器会警告您大多数此类情况,因此您可以利用这些情况

添加以下内容:

-ansi-学究式-W-墙壁式-Werror


这将迫使您修复所有简单的问题。

我发现了错误。该问题源于对浮点型变量的比较。由于精度的原因,比较导致了无限大的错误