1

我编译了我的代码并运行了它,但它给了我分段错误。

所以我使用 valgrid 测试了我的可执行程序。我收到以下消息:

==7932== Invalid read of size 8
==7932==    at 0x491840: IloExpr::operator+=(IloNumLinExprTerm) (in /home/hna/g)
==7932==    by 0x43F261: main (in /home/hna/g)
==7932==  Address 0x8 is not stack'd, malloc'd or (recently) free'd
==7932== 
==7932== 
==7932== Process terminating with default action of signal 11 (SIGSEGV)
==7932==  Access not within mapped region at address 0x8
==7932==    at 0x491840: IloExpr::operator+=(IloNumLinExprTerm) (in /home/hna/g)
==7932==    by 0x43F261: main (in /home/hna/g)
==7932==  If you believe this happened as a result of a stack
==7932==  overflow in your program's main thread (unlikely but
==7932==  possible), you can try to increase the size of the
==7932==  main thread stack using the --main-stacksize= flag.
==7932==  The main thread stack size used in this run was 8388608.
==7932== 
==7932== HEAP SUMMARY:
==7932==     in use at exit: 88,192 bytes in 55 blocks
==7932==   total heap usage: 56 allocs, 1 frees, 88,224 bytes allocated
==7932== 
==7932== LEAK SUMMARY:
==7932==    definitely lost: 0 bytes in 0 blocks
==7932==    indirectly lost: 0 bytes in 0 blocks
==7932==      possibly lost: 0 bytes in 0 blocks
==7932==    still reachable: 88,192 bytes in 55 blocks
==7932==         suppressed: 0 bytes in 0 blocks
==7932== Rerun with --leak-check=full to see details of leaked memory
==7932== 
==7932== For counts of detected and suppressed errors, rerun with: -v
==7932== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)

我的代码是这样的。任何人都可以帮我调试吗?谢谢!

int main()
{   
   int m[8][8];
   m[0][0] = 6; m[1][1] = 6; m[2][2] = 6; m[3][3] = 6;
   m[0][4] = 4; m[1][4] = 4; m[2][5] = 4; m[3][5] = 4;
   m[4][7] = 10; m[5][6] = 10;


IloEnv env;

try {
IloModel model(env);
int n = 4;
int r = 2;
int color = 5;
int N = n*r;
int addupto = 10;

IloArray<IloArray<IloNumVarArray> > x(env, N);  //change to IntVarArray 0~1 to use this for getValues

for(int i=0; i<N; i++) {
    x[i] = IloArray<IloNumVarArray> (env, N);
    for(int j=0; j<N; j++) {
        x[i][j] = IloNumVarArray(env, color);
        for(int k=0; k<color; k++) {
            x[i][j][k] = IloNumVar(env, 0.0, 1.0, ILOINT);
        }
    }
}


IloArray<IloArray<IloExpr> > sumOfColors(env, N);

for(int i=0; i<N; i++) {
    sumOfColors[i] = IloArray<IloExpr>(env, N);
    for(int j=0; j<N; j++) {
        sumOfColors[i][j] = IloExpr(env);
    }
}
for(int i=0; i<N; i++) {
    for(int j=0; j<N; j++) {
        for(int k=0; k<color; k++) {
        sumOfColors[i][j] += x[i][j][k];
        }
    }
}


IloArray<IloArray<IloRange> > range_sumOfColors = IloArray<IloArray<IloRange> >(env, N);

for(int i=0; i<N; i++) {
    range_sumOfColors[i] = IloArray<IloRange> (env, N);
    for(int j=0; j<N; j++) {
        range_sumOfColors[i][j] = IloRange(env, 1, sumOfColors[i][j], 1);
    }
}


IloArray<IloArray<IloExpr> > weightedSumBR = IloArray<IloArray<IloExpr> >(env, r);

for(int i=0; i<r; i++) {
    weightedSumBR[i] = IloArray<IloExpr>(env, r);
    for(int j=0; j<color; j++) {
        for(int k=i*n; k<(i+1)*n; k++) {
            for(int l=0; l<N; l++) {
                 weightedSumBR[i][j] += m[k][l]*x[k][l][j];
            }
        }
    }
}

IloArray<IloArray<IloRange> > range_weightedSumBR(env, r);

for(int i=0; i<r; i++) {
    range_weightedSumBR[i] = IloArray<IloRange>(env, r);
    for(int j=0; j<color; j++) {
        range_weightedSumBR[i][j] = IloRange(env, 0, weightedSumBR[i][j], addupto);
    }
}


IloArray<IloArray<IloExpr> > weightedSumBC(env, r);

for(int i=0; i<r; i++) {
     weightedSumBC[i] = IloArray<IloExpr>(env, r);
     for(int j=0; j<color; j++) {
         for(int k=0; k<N; k++) {
             for(int l=i*n; l<(i+1)*n; l++) {
                 weightedSumBC[i][j] += m[k][l]*x[k][l][j];
             }
         }
     }
}


IloArray<IloArray<IloRange> > range_weightedSumBC(env, r);

for(int i=0; i<r; i++) {
    range_weightedSumBC[i] = IloArray<IloRange>(env, r); 
    for(int j=0; j<color; j++) {
        range_weightedSumBC[i][j] = IloRange(env, 0, weightedSumBC[i][j], addupto);
    }
}

for(int i=0; i<N; i++) {
    for(int j=0; j<N; j++) {
        model.add(range_sumOfColors[i][j]);
    }
}

for(int i=0; i<r; i++) {
    for(int j=0; j<color; j++) {
        model.add(range_weightedSumBR[i][j]);
        model.add(range_weightedSumBC[i][j]);
    }
}

IloCplex cplex(env);
cplex.extract(model);
cplex.solve();
cplex.out() << "solution status = " << cplex.getStatus() << endl;
cplex.out() << endl;



IloArray<IloArray<IloNumArray> > xcp (env, N);  //change to IntVarArray 0~1 to use this for getValues

for(int i=0; i<N; i++) {
    xcp[i] = IloArray<IloNumArray> (env, N);
    for(int j=0; j<N; j++) {
        xcp[i][j] = IloNumArray(env, color);
        for(int k=0; k<color; k++) {
            xcp[i][j][k] = IloNum(env, 0.0, 1.0, ILOINT);
        }
    }
}


    for(int i=0; i<N; i++) {
        for(int j=0; j<N; j++) {
            cplex.out() << cplex.getValues(x[i][j], xcp[i][j]) << endl;
        }
    }


    if (cplex.getStatus() == IloAlgorithm::Infeasible){
        //    cplex.out() << endl << "*** the model is not infeasible ***" << endl;

        ofstream outdata; 
        outdata.open("badmatrix.csv");
        if (!outdata){cerr << "Error: file could not be opened" << endl;
            exit(1);}
        else{
            size_t it1;
            size_t it2;
            size_t n = 8; //m.size();
            for (it1=0;it1<n;it1++){
                for (it2 = 0; it2<n; it2++){
                    outdata << matr[it1][it2] << endl;}}  
        }
        outdata.close();
        env.end();
        cplex.out() << "Infeasible matrix detected" << endl;
        return 1;
    }
}
catch(IloException& e) {
    cerr << " ERROR: " << e << endl;
}
catch(...) {
    cerr << " ERROR: " << endl;
}
env.end();
return 0;
}
4

1 回答 1

2

weightedSumBC[][]将两个索引都定义为范围超过r,但j上升到color

所以改变:

 weightedSumBC[i] = IloArray<IloExpr>(env, r);

到:

 weightedSumBC[i] = IloArray<IloExpr>(env, color);

这里有类似的问题:

for(int i=0; i<r; i++) {
    range_weightedSumBC[i] = IloArray<IloRange>(env, r); 
    for(int j=0; j<color; j++) {
        range_weightedSumBC[i][j] = IloRange(env, 0, weightedSumBC[i][j], addupto);
    }
}

改成:

range_weightedSumBC[i] = IloArray<IloRange>(env, color); 
于 2013-04-09T04:20:51.080 回答