C++ 为什么将此变量设置为false?
我有一个函数,用来检查数独板上某个坐标的可能答案。但是,我只需要您首先关注变量C++ 为什么将此变量设置为false?,c++,boolean,C++,Boolean,我有一个函数,用来检查数独板上某个坐标的可能答案。但是,我只需要您首先关注变量。出于某种原因,first被设置为false,我不知道为什么 功能: void displayPossible(int board[][9], char input[], int &row, int &col) { bool first = true; // variable instantiated and set to true cout &
。出于某种原因,first
被设置为false,我不知道为什么
功能:
void displayPossible(int board[][9], char input[], int &row, int &col)
{
bool first = true; // variable instantiated and set to true
cout << "First " << first << endl;
bool possible[9]; // I dont touch `first` at all
computeValues(board, possible, row, col); // between these two lines..
cout << "First " << first << endl; // by this point it is false. WHY!?
cout << endl;
cout << "Possible: ";
for(int i = 0; i < 9; i++)
cout << possible[i];
cout << endl;
cout << "First " << first << endl;
cout << "The possible values for '" << input << "' are: ";
// if I say 'first = true' right here, i get my expected outcome
for(int i = 0; i < 9; i++)
{
if(possible[i] && first == true)
{
first = false;
cout << i;
}
else if(possible[i] && first == false)
cout << ", " << i;
else
;
}
cout << endl;
}
First 1
First 0
Possible: 000010001
First 0
The possible values for 'd1' are: , 4, 8
void computeValues(int board[][9], bool possible[], int row, int col)
{
for(int i = 0; i < 9; i++)
possible[i] = true;
for(int iRow = 0; iRow < 9; iRow++)
possible[board[iRow][col]] = false;
for(int iCol = 0; iCol < 9; iCol++)
possible[board[row][iCol]] = false;
for(int iRow = 0; iRow < 2; iRow++)
for(int iCol = 0; iCol < 2; iCol++)
possible[board[row/3*3 + iRow][col/3*3 + iCol]] = false;
if(board[row][col] != 0)
possible[board[row][col]] = true;
}
计算值:
void displayPossible(int board[][9], char input[], int &row, int &col)
{
bool first = true; // variable instantiated and set to true
cout << "First " << first << endl;
bool possible[9]; // I dont touch `first` at all
computeValues(board, possible, row, col); // between these two lines..
cout << "First " << first << endl; // by this point it is false. WHY!?
cout << endl;
cout << "Possible: ";
for(int i = 0; i < 9; i++)
cout << possible[i];
cout << endl;
cout << "First " << first << endl;
cout << "The possible values for '" << input << "' are: ";
// if I say 'first = true' right here, i get my expected outcome
for(int i = 0; i < 9; i++)
{
if(possible[i] && first == true)
{
first = false;
cout << i;
}
else if(possible[i] && first == false)
cout << ", " << i;
else
;
}
cout << endl;
}
First 1
First 0
Possible: 000010001
First 0
The possible values for 'd1' are: , 4, 8
void computeValues(int board[][9], bool possible[], int row, int col)
{
for(int i = 0; i < 9; i++)
possible[i] = true;
for(int iRow = 0; iRow < 9; iRow++)
possible[board[iRow][col]] = false;
for(int iCol = 0; iCol < 9; iCol++)
possible[board[row][iCol]] = false;
for(int iRow = 0; iRow < 2; iRow++)
for(int iCol = 0; iCol < 2; iCol++)
possible[board[row/3*3 + iRow][col/3*3 + iCol]] = false;
if(board[row][col] != 0)
possible[board[row][col]] = true;
}
void计算值(int板[][9],布尔可能[],int行,int列)
{
对于(int i=0;i<9;i++)
可能[i]=真;
对于(int-iRow=0;iRow<9;iRow++)
可能的[board[iRow][col]]=false;
对于(int iCol=0;iCol<9;iCol++)
可能的[board[row][iCol]]=false;
对于(int-iRow=0;iRow<2;iRow++)
对于(int iCol=0;iCol<2;iCol++)
可能的[board[row/3*3+iRow][col/3*3+iCol]]=false;
如果(板[行][列]!=0)
可能的[板[行][列]]=true;
}
在我看来,ComputeValues正在访问(错误地)指向&first的指针。在我看来,ComputeValues正在访问(错误地)指向&first的指针。最有可能的是,ComputeValues
的缓冲区溢出正在破坏first
的值。一种明显的可能性是,它使用越界索引写入可能的
。由于可能
和计算值
很可能在堆栈上相邻,这似乎是一种可能的解释。最可能的原因是计算值
存在缓冲区溢出,这会损坏第一个
的值。一种明显的可能性是,它使用越界索引写入可能的
。由于可能
和计算值
很可能在堆栈上彼此相邻,这似乎是一种可能的解释。可能是由于可能值()中的一个bug覆盖了内存中第一个的值。可能是由于可能值()中的bug造成的
重写内存中第一个的值。computeValues
的写入必须超出传递给它的一个数组的末尾(很可能可能)。这会损坏堆栈,覆盖first
的值,并可能导致其他不太明显的破坏。computeValues
必须在传递给它的一个数组的末尾之外写入(很可能可能)。这会破坏堆栈,覆盖first
的值,并可能导致其他不太明显的混乱。几乎可以肯定,因为computeValues
有一个错误,它会覆盖不应该接触的内存,这会影响first
,因为它位于堆栈上可能的旁边。但是如果没有computeValues
的代码,就不可能说。您可能在computeValues
中意外地覆盖了它,例如,通过溢出可能[]
。我们能看看计算值吗
computeValues@kol已添加@jon@rupcomputeValues()
。谢谢你的帮助。@eegaboga:这没什么帮助,因为computeValues
是根据board
中的值来写的,但我们仍然看不到。但是我认为您现在有足够的信息来调试这个问题。几乎可以肯定的是,因为computeValues
有一个错误,它覆盖了它不应该接触的内存,这会首先影响,
,因为它位于堆栈上可能的旁边。但是如果没有computeValues
的代码,就不可能说。您可能在computeValues
中意外地覆盖了它,例如,通过溢出可能[]
。我们能看看计算值吗
computeValues@kol已添加@jon@rupcomputeValues()
。谢谢你的帮助。@eegaboga:这没什么帮助,因为computeValues
是根据board
中的值来写的,但我们仍然看不到。但是我认为你现在有足够的信息来调试这个问题。