tags:

views:

187

answers:

3

I'm having problem with a copying method in a simple C++ program. Everytime I call copy:

Sudoku::SudokuNode** Sudoku::copy(SudokuNode** sudokuBoard)
{
  SudokuNode** tempSudokuBoard = new SudokuNode*[9];
  for(int i = 0; i<9; i++)
  {
   tempSudokuBoard[i] = new SudokuNode[9];
   for(int j = 0; j<9; j++)
   {
    tempSudokuBoard[i][j].currentInteger = sudokuBoard[i][j].currentInteger;
    for(vector<int>::iterator iter = sudokuBoard[i][j].possibleIntegers.begin(); iter!= sudokuBoard[i][j].possibleIntegers.end();)
    {
     tempSudokuBoard[i][j].possibleIntegers.push_back(*iter);
    }
   }
  }
  return tempSudokuBoard;
}

The program seems to completely halt, not returning a a visible error.

If I try to debug the program, the debugger works fine until I arrive at the copy method. Then the debugger displays a dialog box saying:

There is no source code available for the current location.

Any idea what is wrong?

+6  A: 
    for(vector<int>::iterator iter = sudokuBoard[i][j].possibleIntegers.begin(); iter!= sudokuBoard[i][j].possibleIntegers.end();)

You don't seem to be advancing the iterator in that loop, so it will never end. Add ++iter to the counting expression (after the last ; in the for loop).

As to why your debugger can't find source for that location, that's platform dependent. What debugger are you using?

Adam Wright
+2  A: 

You do not increase the iterator on the inside loop:

   for(vector<int>::iterator iter = sudokuBoard[i][j].possibleIntegers.begin(); iter!= sudokuBoard[i][j].possibleIntegers.end(); ++iter)

Resulting an infinate for loop (Compiler knows this and "optimized" it for an infinite loop, which is why there is no code available).

LiraNuna
The answer is mostly correct, but there's no "optimisation" for the infinite loop. It still has meaning, so can't be removed.
Adam Wright
Yes there is, the compiler most likely converted it to `vector<int>::iterator iter = sudokuBoard[i][j].possibleIntegers.begin(); while(1) { ... }`. It's called branch prediction, and GCC (at least) does it well.
LiraNuna
The compiler does NOT know this. The semantic value of .end() can change at any time in the loop body to make it equal to iter. (Elements added or removed, etc).
Chris Kaminski
There is nothing in the loop body that modifies `.end()`, GCC (at least) uses SSA trees to determine that.
LiraNuna
Well, let's assume this is the case, and the code emitted is equivalent to what you claim. It would still have to be either emitted in debug builds, or debug information to compensate. Otherwise, any compiler optimisations at all would render code undebuggable.
Adam Wright
Compilers are not dumb. When debuggers have no code to display it most likely means it got optimized out, or radically changed location.
LiraNuna
Or, far more likely, (s)he's misconfigured the compiler so it doesn't know where to find the source files corresponding to the generated code?
Adam Wright
A: 

I suggest you recode it to use ...

class SudokuBoard
{
  SudokuNode nodes[9];
};
ChrisW