hi all

i use gecode solver to solve my model ; but i get this error

 Access Violation when reading of the location 0x0000001c

and i don't konw how to fix it

here is my code :

#include <gecode/driver.hh>
#include <gecode/int.hh>
#include <gecode/minimodel.hh>
#include <iostream>


#include <vector>
using namespace Gecode;
using namespace std;
class manel : public MaximizeScript {
protected:

  IntVar gain1;

  static const int n=6;//nbre des testeurs 
  static const int m=6;//  nbre des noeuds
        IntVarArray a;

public:
  manel(void)
      : a(*this,m*n,0,1),gain1(*this,0,100000) {

        Matrix <IntVarArray> X (a,m,n);


            vector<int> R1;
            R1.push_back(20);R1.push_back(10);R1.push_back(15);R1.push_back(5);R1.push_back(25);R1.push_back(30);

        vector<int> C1;
            C1.push_back(50);C1.push_back(30);C1.push_back(15);C1.push_back(15);C1.push_back(60);C1.push_back(20);
        vector<int> B1;
            B1.push_back(10);B1.push_back(15);B1.push_back(40);B1.push_back(50);B1.push_back(60);B1.push_back(70);

        vector<int> Dr1;
            Dr1.push_back(1);Dr1.push_back(1);Dr1.push_back(10);Dr1.push_back(5);Dr1.push_back(15);Dr1.push_back(15);
        vector<int> Dc1;
            Dc1.push_back(1);Dc1.push_back(2);Dc1.push_back(20);Dc1.push_back(15);Dc1.push_back(15);Dc1.push_back(15);
        vector<int> Db1;
            Db1.push_back(1);Db1.push_back(2);Db1.push_back(10);Db1.push_back(10);Db1.push_back(20);Db1.push_back(20);
        int g [n][m] ;
            //for(unsigned int i=0; i < n; ++i)
            // g.emplace_back(std::vector<int>(m));
            g[0][0]=125;g[0][1]=150;g[0][2]=1;g[0][3]=0;g[0][4]=0;g[0][5]=0;
            g[1][0]=125;g[1][1]=150;g[1][2]=1;g[1][3]=0;g[1][4]=0;g[1][5]=0;
            g[2][0]=125;g[2][1]=150;g[2][2]=1;g[2][3]=0;g[2][4]=0;g[2][5]=0;
            g[3][0]=125;g[3][1]=150;g[3][2]=1;g[3][3]=0;g[3][4]=0;g[3][5]=0;
            g[4][0]=125;g[4][1]=150;g[4][2]=1;g[4][3]=0;g[4][4]=0;g[4][5]=0;
            g[5][0]=125;g[5][1]=150;g[5][2]=1;g[5][3]=0;g[5][4]=0;g[5][5]=0;
        /*for(int i=0; i<n; i++)
        {
            for (int j =0; j<m;j++)
                g[i][j]=50;
        }*/
            //creation of constraints 
            // ... over rows

        for ( int j=0; j<n;j++)
        {

                linear(*this , X.row(j),IRT_EQ,1);

        }

        //... over columns

                 for (int i = 0; i < m; i++) {

                     linear(*this, Dr1,X.col(i),IRT_LQ,R1[i]);
                                             }

                for (int i = 0; i < m; i++) {
                    linear (*this, Dc1, X.col(i), IRT_LQ,C1[i]);

                                         }

                 for (int i = 0; i <m; i++) {
                     linear (*this, Db1, X.col(i), IRT_LQ,B1[i]);

                                   }



                 //objectiv function
                 IntArgs res(m*n);
                 IntVarArgs X1(m*n);
                 IntVarArgs f(n*m);
                 IntVar z;
                 int count=0;
                  int count1=0;
                  int count2=0;
                  int count3=0;

                 for (int i=0; i<n; i++)
                     {

                         for (int j=0; j<m; j++) 

                         {
                             res[count] = g[i][j];
                             count++;
                             X1[count1] = X(i,j);
                             count1++;
                             rel(*this,z==res[count]*X1[count1]);
                             f[count2]=z;
                             count2++;

                         }
                 }



            // constraint over the objective function    


         linear(*this, f, IRT_EQ, gain);*/

          //Branching
        branch(*this, a, INT_VAR_SIZE_MAX(), INT_VAL_MAX());

  }

  virtual IntVar cost(void) const {
    return gain1;
  }
  /// Print solution
  virtual void print(void) const {
   std::cout << a << std::endl;


  }
  // Constructor for cloning \a s
  manel(bool share, manel& s)
    : MaximizeScript(share,s) {
   // gain.update(*this, share, s.gain);
    a.update(*this, share, s.a);
  }
  // Copy during cloning
  virtual Space* copy(bool share) {
    return new manel(share,*this);
  }


};

int main(int argc, char* argv[]) {
      manel* m = new manel;
   if (manel* s = bab(m)) {
     s->print(); delete s; //Or whatever
}

  system("pause");
  return 0;
}

so can you help me with this think you very much for your help

Dani AI

Generated

Short diagnosis and what caused the crash

The access violation here is almost always caused by undefined behaviour in the model code rather than a Gecode internals bug. In this thread the failure happened while building an arithmetic constraint: multiple index counters were advanced inconsistently and an intermediate IntVar was used before being properly constructed. That combination easily produces out‑of‑bounds reads/writes on IntVarArgs/IntArgs or uses of an invalid IntVar, which in turn leads to the “reading of the location 0x0000001c” crash.

What to change (practical advice)

  • Keep indexing simple and consistent. Use one counter for parallel arrays (coefficients, variable references, result slots) and increment it only after the write you intend. Off‑by‑one mistakes are the most common cause.
  • Ensure every IntVar is constructed with a valid domain or produced through Gecode’s expression API before it is used in a constraint. Do not rely on plain C++ evaluation of mixed int/IntVar expressions.
  • Model dot‑products/sums with a single clear constraint when possible (collect coefficients into an IntArgs and use Gecode’s linear-style constraint) rather than building many ad‑hoc temporary variables. That is both safer and faster.

Debugging checklist

  • Run under gdb or use AddressSanitizer/Valgrind to get the exact failing line.
  • Print or assert the sizes of IntVarArgs/IntArgs before indexing (e.g. check that index < size).
  • Compile with -g -O0 while debugging so stack traces and breakpoints are usable.

Relation to the replies

was right to ask for the failing line, and ’s comment about C++ boolean semantics is relevant: make sure you are building Gecode expressions/constraints, not letting C++ evaluate things to plain bools. The solution posted later by —realigning the indices and producing a proper expression for the product—is the correct fix. Final note: validate indexes and initialize IntVars; that prevents most Gecode access violations.

Recommended Answers

All 5 Replies

On which line is that error occurring?

in this ligne :

rel(*this,z==res[count]*X1[count1]);

i found the one solution :)

   for (int i=0; i<n; i++)
                     {
                         for (int j=0; j<m; j++) 
                         {
                             res[count] = g[i][j];

                             X1[count] = X(i,j);

                             z=expr(*this,res[count]*X1[count]);
                             f[count]=z;
                             count++;
                         }
                 }

rel(this,z==res[count]X1[count1]);

It was probably the == boolean operator that caused the problem

rel(*this,(z=res[count]*X1[count1]));

@Ancient Dragon : thank you very much but it was not the "==" beacuse that's how we post a constraint in the Gecode solvor , it seems that i uses the wrong constraint

Thank you very much for your help

But a c/c++ compiler won't look at it that way. It will pass either 0 or 1 as the parameter because it's a boolean expression (true/false).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.