Hi everyone,
I'm working on a problem from the euler project (#185). anyways, I've written up this "smart" bruteforcing algorithm in C++. now the execution of the algorithm is ugly I know (not a very good programmer) but I've been getting a malloc error and I really can't see why, it's only about 1/2KB. through commenting out code, I've figured out that the error is in this subset of functions. here is a quick setup of the way it's structured:
CODE
class Num{
public:
int value;
bool active;
};
class Guess{
public:
Num list[16];
int numCorrect;
};
CODE
bool crunchLoop( Guess* guesses, int guessesLength, Num* table, int tableLength, int column ){
//backs up the two tables it recieves, this allows it to revert to a previous state
//when it hits a block that can't continue
Guess* guessesBackup = backupGuesses(guesses, guessesLength);
Num* tableBackup = backupTable(table, tableLength);
for(int i=1; i<10; ++i){
if(table[column*i].active == true)
updateTables(guesses, guessesLength, table, tableLength, column, table[column*i].value);
crunchLoop( guesses, guessesLength, table, tableLength, column+1);
}
revertGuesses(guesses, guessesBackup, guessesLength);
revertTable(table, tableBackup, tableLength);
return false;
}
//-----------------------------------------------
Num* backupTable( Num* &table, int tableLength){
//backs up the table for reversion
Num* tableBackup = new Num[tableLength * 10];
for(int i=0; i< tableLength*10; ++i){
tableBackup[i].value = table[i].value;
tableBackup[i].active = table[i].active;
}
return tableBackup;
}
//----------------------------------------------
Guess* backupGuesses( Guess* &guesses, int guessesLength){
Guess* guessesBackup = new Guess[guessesLength*16];
int i = 0;
for(i=0; i < guessesLength; ++i){
for(int j=0; j < 16; j++){
guessesBackup[i].list[j].value = guesses[i].list[j].value;
guessesBackup[i].list[j].active = guesses[i].list[j].active;
}
guessesBackup[i].numCorrect = guesses[i].numCorrect;
}
return guessesBackup;
}
I believe that the malloc is coming from backupGuesses though I can't see what's causing it. if anyone has any suggestions or see's something else that's crippling I'd love for any help I can get. also I can post the entirety of the code, but I warn you it's not pretty.
thanks ;-)