May 20, 2012 at 8:44pm UTC
Hmm this code compiles and runs fine when i set gridsize to a constant, are you sure gridsize has a sensible value after you read it from the file?
EDIT:
also inside that loop, are you trying to access element possibleValues[aa][ab][ac]? That will segfault
Last edited on May 20, 2012 at 8:46pm UTC
May 20, 2012 at 8:47pm UTC
Yes, gridsize is set to 6 when the program reads the file.
May 20, 2012 at 8:51pm UTC
Is this the exact code you are using? Can't reproduce the segfault.
May 20, 2012 at 8:57pm UTC
I removed some other code that I thought was irrelevant to this error, but here's the full code up to the if statement:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
#include <iostream>
#include <fstream>
using namespace std;
int gridsize;
int **grid;
int **zones;
char *operators;
int *totals;
int ***possibleValues;
int zoneCount();
void backtrack(int &aa, int &ab);
bool correct(int a, int b);
void printGrid();
int main(){
ifstream file("k.txt" );
file >> gridsize;
grid = new int *[gridsize];
zones = new int *[gridsize];
operators = new char [gridsize];
totals = new int [gridsize];
possibleValues = new int **[gridsize];
for (int aa=0;aa<gridsize;aa++){
grid[aa] = new int [gridsize];
zones[aa] = new int [gridsize];
possibleValues[aa] = new int *[gridsize];
for (int ab=0;ab<gridsize;ab++){
possibleValues[aa][ab] = new int [gridsize];
}
}
for (int aa=0;aa<gridsize;aa++){
for (int ab=0;ab<gridsize;ab++){
grid[aa][ab]=0;
for (int ac=0;ac<gridsize;ac++){
possibleValues[aa][ab][ac]=ac+1;
}
}
}
for (int aa=0;aa<gridsize;aa++){
for (int ab=0;ab<gridsize;ab++){
for (int ac=0;ac<gridsize;ac++){
cout << possibleValues[aa][ab][ac] << " " ;
}
cout << "\n" ;
}
cout << "\n\n" ;
}
for (int aa=0;aa<gridsize;aa++){
for (int ab=0;ab<gridsize;ab++){
file >> zones[aa][ab];
}
}
for (int aa=0;aa<zoneCount();aa++){
file >> operators[aa];
}
for (int aa=0;aa<zoneCount();aa++){
file >> totals[aa];
}
cout << gridsize;
for (int aa=0;aa<gridsize;aa++){
for (int ab=0;ab<gridsize;ab++){
if (grid[aa][ab] == 0){
for (int ac=1;ac<=gridsize;ac++){
if (possibleValues[aa][ab][ac-1]!=0){
grid[aa][ab] = ac;
possibleValues[aa][ab][ac-1] = 0;
if (correct(aa, ab)){
break ;
}
else if (ac == gridsize){
grid[aa][ab] = 0;
backtrack(aa, ab);
}
}
}
}
}
}
return 0;
}
Last edited on May 20, 2012 at 8:59pm UTC
May 20, 2012 at 9:06pm UTC
What does zoneCount() return?
May 20, 2012 at 9:46pm UTC
In that case you have the problem on line 57 and 60. You try to access 14 elements when both arrays, operators and totals, only have size 6.
Last edited on May 20, 2012 at 9:48pm UTC
May 20, 2012 at 10:16pm UTC
Ah thanks, didn't notice that. I fixed it now :)