Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1034663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:25:39+00:00 2026-05-16T14:25:39+00:00

Today, I encounter a strange problem. My c++ code can work under debug mode.

  • 0

Today, I encounter a strange problem. My c++ code can work under debug mode.
using g++ -g to compile the code. However, when I use g++ -O to optimize the code. It will stuck somewhere. It seems there is dead looping. Does anybody know how to find this kind of error? When I debug the code with DDD debuger, it works fine. Thanks!

The part of code (I find the stuck came from this part) is pasted below:

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

Thanks you for answering my questions. Now my code works without optimizing. I just wonder what the optimizer does thus the code does work. If the optimize mode does not improve the speed much, then it is ok not using it. Can anyone comment on the power of optimize mode?
Thanks again.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T14:25:40+00:00Added an answer on May 16, 2026 at 2:25 pm

    Why is it that at the beginning of the loop you typecasted the solSize to int int solSize=int(curSol_.size()); but didn’t do that in the while loop solSize=curSol_.size();. You might want to investigate the values in the debug and the optimized version.

    Also, there seem to be places in the code where there are floats / doubles being compared with (may be) integers. May be that is the cause of the infinite loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Today I had a strange encounter with gcc. consider the following code: float len[ELEM+1];
Today, I've encountered a strange problem with Groovy code that is run from Maven
I encounter a strange bug - multpart identifier tableabc..fielda cannot be bound today. In
I have learned DFS today and I tried practice tonight. I encounter a problem
I am actually using this calendar source code, but I did encountered a problem
I frequently encounter a code smell when I use the Enumerable group_by method. Some
Today I noticed that the code I have been using to display a FBDialog
I encountered a strange problem today. Whenever i put a breakpoint in one of
I encountered a strange problem today. The good: I successfully changed a global var
Today I encountered something strange: I tried to put a utility method into an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.