1

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?

2
  • you should format your question to make the code more readable Commented Oct 5, 2009 at 19:28
  • Anytime I see a T**, I stop thinking of the code as "simple C++". Commented Oct 7, 2009 at 14:01

3 Answers 3

6
    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?

Sign up to request clarification or add additional context in comments.

Comments

2

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).

7 Comments

The answer is mostly correct, but there's no "optimisation" for the infinite loop. It still has meaning, so can't be removed.
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.
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).
There is nothing in the loop body that modifies .end(), GCC (at least) uses SSA trees to determine that.
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.
|
0

I suggest you recode it to use ...

class SudokuBoard
{
  SudokuNode nodes[9];
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.