Topic
IC4NOTICE: developerWorks Community will be offline May 29-30, 2015 while we upgrade to the latest version of IBM Connections. For more information, read our upgrade FAQ.
6 replies Latest Post - ‏2012-04-16T17:07:36Z by SystemAdmin
FANS_Nader_Al_Theeb
FANS_Nader_Al_Theeb
46 Posts
ACCEPTED ANSWER

Pinned topic Segmentation fault - Using IloExpr

‏2012-04-13T20:45:26Z |
Hello

I got a "Segmentation fault" error when I ran my model in CPLEX - c++ environment, I printed "hello" word after each statement to figure out where the segmentation fault occurs as in the following code
IloExpr cost_Comm(env);
IloExpr cost_Wounded(env);
cout<<"18 hello"<<endl; //18

IloModel model(env);

// First summation

for ( c = 0; c < nbComm; c++){

cout<<"19 hello"<<endl; //19

for( o=0; o<nbDNodes; o++){

cout<<"20 hello"<<endl; //20

for (t=0; t<nbTimes; t++){

cout<<"21 hello"<<endl; //21

cost_Comm += ((p_c [c] * DEV_cot[c][o][t]) / fc[c] );

cout<<"22 hello"<<endl; //22

} } }



I got last hello number 21, and I got "hello" number 19,20, and 21 once. This means that the faults happens just when the cost_Comm expression is used in first iteration.

Any help to solve this problem

Thanks
Updated on 2012-04-16T17:07:36Z at 2012-04-16T17:07:36Z by SystemAdmin
  • SystemAdmin
    SystemAdmin
    7929 Posts
    ACCEPTED ANSWER

    Re: Segmentation fault - Using IloExpr

    ‏2012-04-14T07:40:48Z  in response to FANS_Nader_Al_Theeb
    It would be best to use a debugger to figure out where exactly the segmentation fault occurs.
    I suspect that one of p_c[c], DEV_cot[c][o][t], or fc[c] is not properly initialized and you therefore get a segmentation fault when you try to use it.
    If you do not want to use a debugger, could you print p_c[c], DEV_cot[c][o][t], fc[c] at the same time you print "hello 21"? Do all three values look good? Maybe trying to print them will segfault as well, telling you that indeed one of the three is not initialized properly.
    • FANS_Nader_Al_Theeb
      FANS_Nader_Al_Theeb
      46 Posts
      ACCEPTED ANSWER

      Re: Segmentation fault - Using IloExpr

      ‏2012-04-14T23:12:33Z  in response to SystemAdmin
      Thank you Daniel, fc and p_c are NumArray, so they have values and I can print it before 21 hello. But DEV_cot is NumVarArrayArrayArray, and it doesn't have values

      This could give you more information of why this problem occurs

      Other thing, can you help me about debugging. I'm new in using LINUX - UBUNTU and I don't know how we can debug the code in LINUX enviroment, I think debugging in Windows is easier.

      Thanks
      • SystemAdmin
        SystemAdmin
        7929 Posts
        ACCEPTED ANSWER

        Re: Segmentation fault - Using IloExpr

        ‏2012-04-16T07:41:56Z  in response to FANS_Nader_Al_Theeb
        > FANS_Nader_Al_Theeb wrote:
        > Thank you Daniel, fc and p_c are NumArray, so they have values and I can print it before 21 hello. But DEV_cot is NumVarArrayArrayArray, and it doesn't have values
        >
        But you could still print the DEV_cot elements you are using in order to construct the expression. Just use operator<< to print them:
        
        std::cout << 
        "p_c[" << c << 
        "] = " << p_c[c] << std::endl; std::cout << 
        "fc[" << c << 
        "] = " << fc[c] << std::endl; std::cout << 
        "DEV_cot[" << c << 
        "," << o << 
        "," << t << 
        "] = " <<  DEV_cot[c][o][t] << std::endl;
        

        What is the output of this right before the program crashes?

        >
        > Other thing, can you help me about debugging. I'm new in using LINUX - UBUNTU and I don't know how we can debug the code in LINUX enviroment, I think debugging in Windows is easier.
        >
        I think Linux has much more powerful debugging tools but there is no point discussing this here :-)
        You could use gdb for debugging. Just do
        
        gdb --args <your-program> <program arguments>
        

        This will start the interactive command line debugger. At the prompt of this debugger type 'run' to start your program. Once the program crashes you will be back at the prompt. At this time type 'backtrace' to see a full stack trace. Maybe post this trace here together with the relevant parts of your code.
        Also refer to the tutorials for gdb to learn how to use it. There also is 'ddd' which is a debugger with a graphical user interface. Maybe this is easier for you to use.
        • FANS_Nader_Al_Theeb
          FANS_Nader_Al_Theeb
          46 Posts
          ACCEPTED ANSWER

          Re: Segmentation fault - Using IloExpr

          ‏2012-04-16T16:48:01Z  in response to SystemAdmin
          Hi, Thank you Daniel for your help

          the output before the program crashed is

          p_c[0] = 20
          fc[0] = 20
          DEV_cot0,0,0 =
          Segmentation fault

          could this help?

          Regards
          • FANS_Nader_Al_Theeb
            FANS_Nader_Al_Theeb
            46 Posts
            ACCEPTED ANSWER

            Re: Segmentation fault - Using IloExpr

            ‏2012-04-16T16:53:51Z  in response to FANS_Nader_Al_Theeb
            Hi, sorry for replaying twice, but I want to show the DEV_cot definition which could also help

            IloNumVarArray3 DEV_cot(env,nbComm);
            for( c = 0; c < nbComm; c++)
            {
            DEV_cot[c] = IloNumVarArray2 (env, nbDNodes);
            for( o=0; o<nbDNodes; o++)
            {
            DEV_cot[c][o] = IloNumVarArray (env, nbTimes); } }

            // then in defintion of objective, I have

            IloExpr cost_Comm(env);

            for ( c = 0; c < nbComm; c++){
            for( o=0; o<nbDNodes; o++){
            for (t=0; t<nbTimes; t++){

            std::cout << "p_c = " << p_c[c] << std::endl;
            std::cout << "fc = " << fc[c] << std::endl;
            std::cout << "DEV_cot = " << DEV_cot[c][o][t] << std::endl;

            cost_Comm += ((p_c [c] * DEV_cot[c][o][t]) / fc[c] ); } } }

            // and finally I have the following obj

            model.add(IloMinimize(env, cost_Comm ));
            cost_Comm.end()



            Thanks
            • SystemAdmin
              SystemAdmin
              7929 Posts
              ACCEPTED ANSWER

              Re: Segmentation fault - Using IloExpr

              ‏2012-04-16T17:07:36Z  in response to FANS_Nader_Al_Theeb
              That indeed does help. The problem is that the elements in DEV_cot[c][o] are not initialized. You use the constructor
              
              IloNumVarArray(
              
              const IloEnv env, IloInt n=0)
              

              which according to the reference documentation does not initialize the elements in the created array (they are all empty handles).
              To initialize the elements in the array either use one more loop:
              
              
              
              for (IloInt t = 0; t < nbTimes; ++t) DEV_cot[c][o][t] = IloNumVar(env);
              

              or use one of the constructors that initialize the elements in the array. The eligible constructors are
              
              IloNumVarArray(
              
              const IloEnv env, 
              
              const IloNumArray lb, 
              
              const IloNumArray ub, IloNumVar::Type type=ILOFLOAT) IloNumVarArray(
              
              const IloEnv env, IloNum lb, 
              
              const IloNumArray ub, IloNumVar::Type type=ILOFLOAT) IloNumVarArray(
              
              const IloEnv env, 
              
              const IloNumArray lb, IloNum ub, IloNumVar::Type type=ILOFLOAT) IloNumVarArray(
              
              const IloEnv env, IloInt n, IloNum lb, IloNum ub, IloNumVar::Type type=ILOFLOAT)
              

              (see there reference documentation for further details).