How to fix "0xC0000005: Access violation reading location" in C++

-1

I've been learning how to write optimization programming in c++ recently. I kept receiving the same message once I build my optimization model:

enter image description here

Can someone help me or tell me what's going on there? Thanks a lot.

#include <ilcplex/ilocplex.h> ILOSTLBEGIN

typedef IloArray<IloNumVarArray> IloNumVarArray2;

int main(int argc, char ∗∗argv) {
  IloEnv env;

  try {
    const char;

    IloInt i, j;
    IloModel mod(env);

    IloNumVarArray2 x(env);
    for (i = 0; i < 3; i++) {
      x.add(IloNumVarArray(env, 2, 0.0, IloInfinity));
    }

    mod.add(IloMaximize(
        env, 6.5 * x[0][0] + 11 * x[0][1] + 9.75 * x[1][0] + 12.25 * x[1][1] +
                 9.5 * x[1][2] + 4.75 * x[2][0] + 7.75 * x[2][1] +
                 8.5 * x[2][2] + 7.5 * x[3][0] + 8.5 * x[3][1]));
    mod.add(x[0][0] + x[1][0] + x[2][0] + x[3][0] >= 500);
    mod.add(x[0][1] + x[1][1] + x[2][1] + x[3][1] >= 600);
    mod.add(x[0][2] + x[1][2] + x[2][2] + x[3][2] >= 500);

    mod.add(x[0][0] + x[0][1] <= 600);
    mod.add(x[1][0] + x[1][1] + x[1][2] <= 500);
    mod.add(x[2][0] + x[2][1] + x[2][2] <= 300);
    mod.add(x[3][0] + x[3][1] <= 400);

    IloCplex cplex(mod);

    if (!cplex.solve()) {
      env.error() << "Failed to optimize LP." << endl;
      throw(-1);
    }

    IloNumArray vals(env);
    env.out() << "Solution status = " << cplex.getStatus() << endl;
    env.out() << "Solution value = " << cplex.getObjValue() << endl;
    env.out() << "Values = " << vals << endl;

  } catch (IloException &e) {
    cerr << "Error 2" << e << endl;
  } catch (...) {
    cerr << "Error 1" << endl;
  }
  env.end();

  return 0;
}
c++
cplex
asked on Stack Overflow Apr 16, 2020 by qw Epis • edited Apr 16, 2020 by qw Epis

1 Answer

1

Like people in the comments suggested, use a debugger to track down the problem. Even better: compile your program in debug mode. The IloCplex code contains a good number of assertions in the header files that catch common errors. Running your code in debug mode gives this assertion failure:

X& IloArray::operator[] (IloInt i) : Out of bounds operation: index superior to size of array
segfault2.bin++: concert/include/ilconcert/iloenv.h:2246: X& IloArray<X>::operator[](IloInt) [with X = IloNumVarArray; IloInt = long int]: Assertion `(i < _impl->getSize()) || (std:: cerr << "X& IloArray::operator[] (IloInt i) : Out of bounds operation: index superior to size of array" << std:: endl, ilo_stop_assert())' failed.
Aborted

This clearly shows that you are accessing an array out of its bounds (like one of the comments already suggests). Tracing this back, you can see that the offending line in your code is:

mod.add(IloMaximize(
    env, 6.5 * x[0][0] + 11 * x[0][1] + 9.75 * x[1][0] + 12.25 * x[1][1] +
             9.5 * x[1][2] + 4.75 * x[2][0] + 7.75 * x[2][1] +
             8.5 * x[2][2] + 7.5 * x[3][0] + 8.5 * x[3][1]));

You are accesing x[3][*] although the x array has only 3 elements. So only indices 0, 1, 2 are valid for this array. Also, you are accessing x[*][2] in some places, although the second dimension of your array is only 2.

answered on Stack Overflow Apr 17, 2020 by Daniel Junglas

User contributions licensed under CC BY-SA 3.0