为什么此代码显示分段错误(核心转储)? [英] Why is this code showing segmentation fault (core dumped ) ?

查看:80
本文介绍了为什么此代码显示分段错误(核心转储)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有更好的方式提交所有这些信息,或者如果这是错误的论坛,我很抱歉,我是这个网站的新手。



我用C ++创建了一个Sudoku Solver,它可以解决9x9 Sudoku的n次迭代,或者通过提供负数来解决直到拼图完成。用户可以手动输入数独谜题的数字或使用文件重定向输入数独谜题文本文件。

 $ user ./a.out< input.txt 



我面临的问题是我的求解器会解决一些谜题,但对于其他谜题,它会返回分段错误(核心转储)。



我无法理解可能导致此错误的原因。


它可以解决这两个难题

 2 0 7 0 9 0 4 0 6 
0 4 0 2 6 7 0 5 3
5 0 0 3 8 4 0 0 0
6 0 0 5 2 8 3 0 7
8 2 5 0 3 1 0 0 9
0 0 1 9 4 6 0 2 0
4 0 2 6 1 9 7 0 8
9 1 3 8 0 0 0 0 4
7 0 0 0 5 3 1 9 2



 0 0 5 7 1 0 6 8 2 
0 3 6 0 4 0 0 7 5
7 0 0 5 8 6 3 4 9
9 8 4 0 5 7 2 0 0
1 5 0 0 0 0 7 9 8
6 7 2 8 0 0 0 0 4
0 6 8 2 0 9 0 1 0
3 4 9 1 0 0 8 2 0
0 1 7 0 3 0 9 0 6





但是例如这两个谜题会返回分段错误(核心)倾销)

 3 0 0 2 0 0 0 0 0 
0 0 0 1 0 7 0 0 0
7 0 6 0 3 0 5 0 0
0 7 0 0 0 9 0 8 0
9 0 0 0 2 0 0 0 4
0 1 0 8 0 0 0 5 0
0 0 9 0 4 0 3 0 1
0 0 0 7 0 2 0 0 0
0 0 0 0 0 8 0 0 6





<前郎=text> 0 0 0 0 0 3 0 1 7
0 1 5 0 0 9 0 0 8
0 6 0 0 0 0 0 0 0
1 0 0 0 0 7 0 0 0
0 0 9 0 0 0 2 0 0
0 0 0 5 0 0 0 0 4
0 0 0 0 0 0 0 0 0
5 0 0 6 0 0 3 4 0
3 4 0 2 0 0 0 0 0





使用调试器时(带有谜题)不起作用)程序在我的求解函数中的第一个检查方法停止并返回错误。



  //  调试器返回错误的位置 
bool mySudoku :: CheckRow( int 行, int number){ // 检查用户输入的行数字
for int i = 0 ; i< SIZE; i ++) // 如果找到数字则返回
{ // 如果不是,则为false
if (Data [row] [i] == number)
{
return ;
}
}
返回 false ;
}





这是我的.cpp文件

  #include     mySudoku.h 
#include < cstdlib >
#include < iostream >

使用 namespace std;

mySudoku :: mySudoku(){ // 默认构造函数
for int row = 0 ; row< SIZE; row ++)
{
for int col = < span class =code-digit> 0 ; col< SIZE; col ++)
{
Data [row] [col] = 0 ;
}
}
// 将数据的数据初始化为全0
}

mySudoku :: mySudoku( int anArray [] [SIZE]){ // 替代构造函数
int row = 0 ; row< SIZE; row ++)
{
for int col = 0 ; col< SIZE; col ++)
{
数据[row] [col] = anArray [row] [col];
}
}
}

mySudoku :: mySudoku( const mySudoku& orig){ // 复制构造函数

}

mySudoku :: ~mySudoku(){
// 析构函数
}

void mySudoku :: ReadPuzzle(){ // 阅读拼图
cout<< 请输入拼图网格的值<< endl;
for int row = 0 ; row< SIZE; row ++)
{
for int col = 0 ; col< SIZE; col ++)
{
cout<< Row<< row + 1<< Col<< col + 1<< ENDL;
cin>>数据[行] [山口];
}
}
cout<< 您已完成输入网格的值!<< ENDL;
}

void mySudoku :: PrintPuzzle(){ // 打印拼图
int row = 0 ; row< SIZE; row ++)
{
for int col = 0 ; col< SIZE; col ++)
{
cout<<数据[row] [col]<< ;
}

cout<< ENDL;
}
}

bool mySudoku :: CheckCol( int col, int number){ // check用户输入数字的col
int i = 0 ; i< SIZE; i ++) // 如果找到数字则返回true
{ // 如果不是,则为false
if (Data [i] [col] == number)
{
return ;
}
}
返回 false ;
}

bool mySudoku :: CheckRow( int 行, int number){ // 检查用户输入的号码行
for int i = 0 ; i< SIZE; i ++) // 如果找到数字则返回
{ // 如果不是,则为false
if (Data [row] [i] == number)
{
return ;
}
}
返回 false ;
}

bool mySudoku :: CheckSubBox( int 行, int col, int number){ // 检查用户输入数字的子框
int startIndexRow = row-row%3; // 如果找到数字,则返回true
int startIndexCol = col-col%3; // 如果不是,则为false

for int i = 0 ; i< 3; i ++)
{
for int j = 0 ; j< 3; j ++)
{
if (Data [startIndexRow + i] [startIndexCol + j] == number)
{
return true ;
}
}
}
返回 false ;
}

bool mySudoku :: CheckForUnassigned() // 检查未分配的值
{ // 哪个表示为零
int i = 0 ; i< SIZE; i ++) // 如果存在未分配的值,则返回true
{ // 如果不是,则为false
for int j = 0 ; j< SIZE; j ++ )
{
if (Data [i] [j] == 0
{
return false ;
}
}
}
返回 true ;

}

int mySudoku :: SolvePuzzle( int iterations){ // 解决两个条件下的难题
int counter = 0 ; // 按一定的迭代次数
while (iterations!= 0 // 或直到收敛
{
for int i = 0 ; i< SIZE; i ++)
{
for int j = 0 ; j< SIZE; j ++)
{
if (Data [i] [j] == 0
{
int ansCount = 0 ;
int ans = 0 ;
for int k = 1 ; k< SIZE + 1; k ++)
{
if ((CheckRow(i,k)== false )&&(CheckCol(j,k)== false )&&(CheckSubBox(i, j,k)== false ))
{
ansCount ++;
ans = k;
}

}
如果(ansCount == 1
{
Data [i] [j] = ans;
}
}

}
}
如果(迭代> 0
{
iterations--;
}
else if ((iterations< 0 )&&(CheckForUnassigned()== true ))
{
counter ++;
返回计数器;
}
else if ((iterations< 0 )&&(CheckForUnassigned()== false ))
{
counter ++;
SolvePuzzle(迭代);
}

counter ++;

}
return counter;

}





这是我的主文件

  #include   <   cstdlib  >  
#include < iostream >
#include mySudoku.h

使用 命名空间标准;

/ *
*
* /

int main( int argc, char ** argv){
// 初始化测试数组
int testArray [ 9 ] [ 9 ];

// 填充数组
for int row = 0 ; row< 9; row ++)
{
for int col = 0 ; col< 9; col ++)
{
testArray [row] [col] = 0 ;
}
}


// 定义对象mySudoku
mySudoku Stest1;
mySudoku Stest2(testArray);


Stest1.ReadPuzzle(); // 阅读拼图
Stest1.CheckCol( 1 3 ); // 检查列
Stest1.CheckRow( 1 3 ); // 检查行
Stest1.CheckSubBox( 4 4 5 ); // check 3x3 subbox
cout<< iteration:<< Stest1.SolvePuzzle( - 1 )<< ENDL; // 解决拼图测试
Stest1.PrintPuzzle(); // 打印拼图
cout<< \ n;
Stest2.PrintPuzzle();

return 0 ;
}







和我的.h文件



  #ifndef MYSUDOKU_H 
#define MYSUDOKU_H

const int SIZE( 9 ); // 常量

class mySudoku {
public
mySudoku(); // 默认构造函数
mySudoku( int anArray [] [SIZE]); // 替代构造函数
mySudoku( const mySudoku& orig); // 复制构造函数

// function
void ReadPuzzle( );
void PrintPuzzle();
int SolvePuzzle( int 次迭代);
bool CheckRow( int 行, int 数字);
bool CheckCol( int col, int 数字);
bool CheckSubBox( int 行, int col, int number);
bool CheckForUnassigned();

virtual ~mySudoku();
private
int 数据[SIZE] [SIZE];
};

#endif / * MYSUDOKU_H * /

解决方案

user ./a.out< input.txt



我面临的问题是我的求解器会解决一些谜题,但对于其他谜题,它会返回分段错误(核心转储)。



我无法理解可能导致此错误的原因。


它可以解决这两个难题

 2 0 7 0 9 0 4 0 6 
0 4 0 2 6 7 0 5 3
5 0 0 3 8 4 0 0 0
6 0 0 5 2 8 3 0 7
8 2 5 0 3 1 0 0 9
0 0 1 9 4 6 0 2 0
4 0 2 6 1 9 7 0 8
9 1 3 8 0 0 0 0 4
7 0 0 0 5 3 1 9 2



 0 0 5 7 1 0 6 8 2 
0 3 6 0 4 0 0 7 5
7 0 0 5 8 6 3 4 9
9 8 4 0 5 7 2 0 0
1 5 0 0 0 0 7 9 8
6 7 2 8 0 0 0 0 4
0 6 8 2 0 9 0 1 0
3 4 9 1 0 0 8 2 0
0 1 7 0 3 0 9 0 6





但是例如这两个谜题会返回分段错误(核心)倾销)

 3 0 0 2 0 0 0 0 0 
0 0 0 1 0 7 0 0 0
7 0 6 0 3 0 5 0 0
0 7 0 0 0 9 0 8 0
9 0 0 0 2 0 0 0 4
0 1 0 8 0 0 0 5 0
0 0 9 0 4 0 3 0 1
0 0 0 7 0 2 0 0 0
0 0 0 0 0 8 0 0 6





<前郎=text> 0 0 0 0 0 3 0 1 7
0 1 5 0 0 9 0 0 8
0 6 0 0 0 0 0 0 0
1 0 0 0 0 7 0 0 0
0 0 9 0 0 0 2 0 0
0 0 0 5 0 0 0 0 4
0 0 0 0 0 0 0 0 0
5 0 0 6 0 0 3 4 0
3 4 0 2 0 0 0 0 0





使用调试器时(带有谜题)不起作用)程序在我的求解函数中的第一个检查方法停止并返回错误。



  //  调试器返回错误的位置 
bool mySudoku :: CheckRow( int 行, int number){ // 检查用户输入的行数字
for int i = 0 ; i< SIZE; i ++) // 如果找到数字则返回
{ // 如果不是,则为false
if (Data [row] [i] == number)
{
return ;
}
}
返回 false ;
}





这是我的.cpp文件

  #include     mySudoku.h 
#include < cstdlib >
#include < iostream >

使用 namespace std;

mySudoku :: mySudoku(){ // 默认构造函数
for int row = 0 ; row< SIZE; row ++)
{
for int col = < span class =code-digit> 0 ; col< SIZE; col ++)
{
Data [row] [col] = 0 ;
}
}
// 将数据的数据初始化为全0
}

mySudoku :: mySudoku( int anArray [] [SIZE]){ // 替代构造函数
int row = 0 ; row< SIZE; row ++)
{
for int col = 0 ; col< SIZE; col ++)
{
数据[row] [col] = anArray [row] [col];
}
}
}

mySudoku :: mySudoku( const mySudoku& orig){ // 复制构造函数

}

mySudoku :: ~mySudoku(){
// 析构函数
}

void mySudoku :: ReadPuzzle(){ // 阅读拼图
cout<< 请输入拼图网格的值<< endl;
for int row = 0 ; row< SIZE; row ++)
{
for int col = 0 ; col< SIZE; col ++)
{
cout<< Row<< row + 1<< Col<< col + 1<< ENDL;
cin>>数据[行] [山口];
}
}
cout<< 您已完成输入网格的值!<< ENDL;
}

void mySudoku :: PrintPuzzle(){ // 打印拼图
int row = 0 ; row< SIZE; row ++)
{
for int col = 0 ; col< SIZE; col ++)
{
cout<<数据[row] [col]<< ;
}

cout<< ENDL;
}
}

bool mySudoku :: CheckCol( int col, int number){ // check用户输入数字的col
int i = 0 ; i< SIZE; i ++) // 如果找到数字则返回true
{ // 如果不是,则为false
if (Data [i] [col] == number)
{
return ;
}
}
返回 false ;
}

bool mySudoku :: CheckRow( int 行, int number){ // 检查用户输入的号码行
for int i = 0 ; i< SIZE; i ++) // 如果找到数字则返回
{ // 如果不是,则为false
if (Data [row] [i] == number)
{
return ;
}
}
返回 false ;
}

bool mySudoku :: CheckSubBox( int 行, int col, int number){ // 检查用户输入数字的子框
int startIndexRow = row-row%3; // 如果找到数字,则返回true
int startIndexCol = col-col%3; // 如果不是,则为false

for int i = 0 ; i< 3; i ++)
{
for int j = 0 ; j< 3; j ++)
{
if (Data [startIndexRow + i] [startIndexCol + j] == number)
{
return true ;
}
}
}
返回 false ;
}

bool mySudoku :: CheckForUnassigned() // 检查未分配的值
{ // 哪个表示为零
int i = 0 ; i< SIZE; i ++) // 如果存在未分配的值,则返回true
{ // 如果不是,则为false
for int j = 0 ; j< SIZE; j ++ )
{
if (Data [i] [j] == 0
{
return false ;
}
}
}
返回 true ;

}

int mySudoku :: SolvePuzzle( int iterations){ // 解决两个条件下的难题
int counter = 0 ; //by a certain number of iterations
while(iterations != 0)\t\t\t\t\t //or until convergence
{
\tfor(int i=0; i < SIZE; i++)
\t{
\t\tfor(int j=0; j < SIZE; j++)
\t\t{
\t\t\tif(Data[i][j]==0)
\t\t\t{
\t\t\t\tint ansCount=0;
\t\t\t\tint ans =0;
\t\t\t\tfor(int k=1; k < SIZE+1; k++)
\t\t\t\t{
\t\t\t\t\tif((CheckRow(i,k)==false)&&(CheckCol(j,k)==false)&&(CheckSubBox(i,j,k)==false))
\t\t\t\t\t{
\t\t\t\t\t\tansCount++;
\t\t\t\t\t\tans = k;
\t\t\t\t\t}
\t\t\t\t\t
\t\t\t\t}
\t\t\t\tif(ansCount == 1)
\t\t\t\t{
\t\t\t\t\tData[i][j] = ans;
\t\t\t\t}
\t\t\t}

\t\t}
\t}
\tif(iterations > 0)
\t{
\t\titerations--;
\t}
\telse if((iterations < 0) && (CheckForUnassigned()==true))
\t{
\t\tcounter++;
\t\treturn counter;
\t}
\telse if((iterations < 0) && (CheckForUnassigned()==false))
\t{
\t\tcounter++;
\t\tSolvePuzzle(iterations);
\t}

\tcounter++;

}
return counter;

}





Here is my main file

#include <cstdlib> 
#include <iostream>
#include \"mySudoku.h\"

using namespace std;

/*
*
*/

int main(int argc, char** argv) {
//initialize test array
int testArray[9][9];

//populate array
for(int row=0; row<9; row++)
{
for(int col=0; col<9; col++)
{
testArray[row][col] = 0;
}
}


//define object mySudoku
mySudoku Stest1;
mySudoku Stest2(testArray);


Stest1.ReadPuzzle(); //read puzzle
Stest1.CheckCol(1,3); //check column
Stest1.CheckRow(1,3); //check row
Stest1.CheckSubBox(4,4,5); //check 3x3 subbox
cout << \" iteration: \" << Stest1.SolvePuzzle(-1) << ENDL; //solves puzzle test
Stest1.PrintPuzzle(); // print puzzle
cout << \ n;
Stest2.PrintPuzzle();

return 0;
}







And my .h file



#ifndef MYSUDOKU_H 
#define\tMYSUDOKU_H

const int SIZE(9); //constant

class mySudoku {
public:
mySudoku(); //default constructor
mySudoku(int anArray[][SIZE]); //alternate constructor
mySudoku(const mySudoku& orig);//copy constructor

//functions
void ReadPuzzle();
void PrintPuzzle();
int SolvePuzzle(int iterations);
bool CheckRow(int row, int number);
bool CheckCol(int col, int number);
bool CheckSubBox(int row, int col, int number);
\tbool CheckForUnassigned();

virtual ~mySudoku();
private:
int Data[SIZE][SIZE];
};

#endif\t/* MYSUDOKU_H */


This exception is raised in most cases when your application goes outside of the segment it was assignment, and tries to overwrite or interact with the memory areas that it does not have access to.



I am not sure where that \"SIZE\" variable (or constant) comes from or what you are doing with it, be the problem is clearly an invalid access or unauthorized access to a memory location where your program does not have access to propagate. Your program may be accessing i from outside the bounds because of this SIZE variable.



You mentioned, the problem is here:



if(Data[row][i] == number) 
{
return true;
}





Maybe, now do yourself a favor, and instead of going deeper in debugging. Re-read the statements,



if, ith element in rowth array of Data is equal to number, then return true.



Now, program would do the same thing. It would try to capture the ith element, from the rowth array in your Data object (where Data is a multi-dimen array). If that is accessible, then it would compare, otherwise if the memory is protected, is not available to your program, is authorized to another program, kernel would simply raise this error and program would terminate. Anyways, read the following resources to understand the problem and solve it by overcoming the causes. :-)



For more please refer to these threads:



http://stackoverflow.com/questions/19641597/what-is-segmentation-fault-core-dumped[^]

http://ubuntuforums.org/showthread.php?t=2071988[^]

http://stackoverflow.com/questions/2346806/what-is-segmentation-fault[^]



h ttps://en.wikipedia.org/wiki/Segmentation_fault[^]


My apologies if there is a better way to submit all this information or if this is the wrong forum, I am new to this site.

I have created a Sudoku Solver in C++ which can solve a 9x9 Sudoku for n number of iterations or solve until the puzzle is complete by providing a negative number. The user has the ability to manually input the numbers for the Sudoku puzzle or input a sudoku puzzle text file using file redirection.

$user ./a.out < input.txt


The problem I am facing is that my solver will solve for some puzzles but for others it returns segmentation fault (core dumped).

I am having trouble understanding what could cause this error.

It can solve these two puzzles

2 0 7 0 9 0 4 0 6     
0 4 0 2 6 7 0 5 3
5 0 0 3 8 4 0 0 0
6 0 0 5 2 8 3 0 7
8 2 5 0 3 1 0 0 9
0 0 1 9 4 6 0 2 0
4 0 2 6 1 9 7 0 8
9 1 3 8 0 0 0 0 4
7 0 0 0 5 3 1 9 2


0 0 5 7 1 0 6 8 2
0 3 6 0 4 0 0 7 5
7 0 0 5 8 6 3 4 9
9 8 4 0 5 7 2 0 0
1 5 0 0 0 0 7 9 8
6 7 2 8 0 0 0 0 4
0 6 8 2 0 9 0 1 0
3 4 9 1 0 0 8 2 0
0 1 7 0 3 0 9 0 6



but for example these two puzzles it will return segmentation fault (core dumped)

3 0 0 2 0 0 0 0 0
0 0 0 1 0 7 0 0 0
7 0 6 0 3 0 5 0 0
0 7 0 0 0 9 0 8 0
9 0 0 0 2 0 0 0 4
0 1 0 8 0 0 0 5 0
0 0 9 0 4 0 3 0 1
0 0 0 7 0 2 0 0 0
0 0 0 0 0 8 0 0 6



0 0 0 0 0 3 0 1 7
0 1 5 0 0 9 0 0 8
0 6 0 0 0 0 0 0 0
1 0 0 0 0 7 0 0 0
0 0 9 0 0 0 2 0 0 
0 0 0 5 0 0 0 0 4
0 0 0 0 0 0 0 2 0
5 0 0 6 0 0 3 4 0 
3 4 0 2 0 0 0 0 0



When using a debugger ( with the puzzles that don't work ) the program stops at my first check method in my solve function and returns the error.

//where the debugger returns the error 
bool mySudoku::CheckRow(int row, int number){ //check row for user inputted number 
    	for(int i=0; i< SIZE; i++)            //return true if number is found
	{                                     //false if not
            if(Data[row][i] == number)
            {
				return true;
            }
	}
    return false;
}



Here is my .cpp file

#include "mySudoku.h"
#include <cstdlib>
#include <iostream> 

using namespace std;

mySudoku::mySudoku(){ //default constructor 
    for(int row=0; row < SIZE; row++)
    {
        for(int col=0; col < SIZE; col++)
        {
            Data[row][col] = 0;
        }
    }
    //initalize Array's data to all 0's
}

mySudoku::mySudoku(int anArray[][SIZE]){ //alternate constructor 
    for(int row=0; row < SIZE; row++)
    {
        for(int col=0; col < SIZE; col++)
        {
            Data[row][col] = anArray[row][col];
        }
    }
}

mySudoku::mySudoku(const mySudoku& orig){ //copy constructor
    
}

mySudoku::~mySudoku(){
    //destructor
}

void mySudoku::ReadPuzzle(){  //read the puzzle 
    cout << "Please input values for grid of the puzzle" <<endl;
    for(int row=0;row< SIZE; row++)
    {
        for(int col=0; col< SIZE; col++)
        {
			cout << "Row " << row+1 << " Col " << col+1 << endl;
            cin >> Data[row][col];
        }
    }
    cout << "you have completed inputting values for the grid!" << endl;
}

void mySudoku::PrintPuzzle(){ //prints the puzzle 
    for(int row=0; row < SIZE; row++)
    {
        for(int col=0; col < SIZE; col++)
        {
            cout << Data[row][col] << " ";
        }
        
        cout << endl;
    }
}

bool mySudoku::CheckCol(int col, int number){  //check col for user inputted number
	for(int i=0;i< SIZE; i++)               //return true if number is found 
	{                                       //false if not 
            if(Data[i][col] == number)
            {
                return true;
            }
	}
    return false;
}

bool mySudoku::CheckRow(int row, int number){ //check row for user inputted number 
    	for(int i=0; i< SIZE; i++)            //return true if number is found
	{                                     //false if not
            if(Data[row][i] == number)
            {
				return true;
            }
	}
    return false;
}

bool mySudoku::CheckSubBox(int row, int col, int number){ //check sub box for user inputted number 
    int startIndexRow = row-row%3;					      //return true if number is found
    int startIndexCol = col-col%3;						  //false if not
    
    for(int i=0; i<3; i++)
    {
        for(int j=0; j<3; j++)
        {
            if(Data[startIndexRow+i][startIndexCol+j] == number)
            {
                return true; 
            }
        }
    }
    return false; 
}

bool mySudoku::CheckForUnassigned() //checks for unassigned values 
{									//which are denoted by a zero 
	for(int i=0; i<SIZE;i++)		//return true if unassigned value exist 
	{								//false if not 
		for(int j=0; j<SIZE; j++)
		{
			if(Data[i][j] == 0)
			{
				return false;
			}
		}
	}
	return true;

}

int mySudoku::SolvePuzzle(int iterations){ //solves the puzzle on two conditions 
int counter=0;							   //by a certain number of iterations 
while(iterations != 0)					   //or until convergence 
{
	for(int i=0; i < SIZE; i++) 
	{
		for(int j=0; j < SIZE; j++) 
		{
			if(Data[i][j]==0)
			{
				int ansCount=0;
				int ans =0;
				for(int k=1; k < SIZE+1; k++)
				{
					if((CheckRow(i,k)==false)&&(CheckCol(j,k)==false)&&(CheckSubBox(i,j,k)==false))
					{
						ansCount++;
						ans = k;
					}
					
				}
				if(ansCount == 1)
				{
					Data[i][j] = ans;
				}
			}

		}
	}
	if(iterations > 0)
	{
		iterations--;
	}
	else if((iterations < 0) && (CheckForUnassigned()==true))
	{
		counter++; 
		return counter; 
	}
	else if((iterations < 0) && (CheckForUnassigned()==false))
	{
		counter++;
		SolvePuzzle(iterations);
	}

	counter++;

}
 return counter;

}



Here is my main file

#include <cstdlib>
#include <iostream>
#include "mySudoku.h"

using namespace std;

/*
 * 
 */
int main(int argc, char** argv) {
    //initialize test array
    int testArray[9][9]; 
    
    //populate array
    for(int row=0; row<9; row++)
    {
        for(int col=0; col<9; col++)
        {
            testArray[row][col] = 0;
        }
    }
    
    
    //define object mySudoku 
    mySudoku Stest1;
    mySudoku Stest2(testArray);
    
    
    Stest1.ReadPuzzle();    //read puzzle 
    Stest1.CheckCol(1,3);  //check column 
    Stest1.CheckRow(1,3);  //check row
    Stest1.CheckSubBox(4,4,5); //check 3x3 subbox
    cout << " iteration: " << Stest1.SolvePuzzle(-1) << endl;  //solves puzzle test 
    Stest1.PrintPuzzle();   // print puzzle 
    cout << "\n";
    Stest2.PrintPuzzle();
   
    return 0;
}




And my .h file

#ifndef MYSUDOKU_H
#define	MYSUDOKU_H

const int SIZE(9); //constant 

class mySudoku {
public:
    mySudoku(); //default constructor 
    mySudoku(int anArray[][SIZE]); //alternate constructor 
    mySudoku(const mySudoku& orig);//copy constructor 

    //functions 
    void ReadPuzzle(); 
    void PrintPuzzle(); 
    int SolvePuzzle(int iterations);
    bool CheckRow(int row, int number);
    bool CheckCol(int col, int number);
    bool CheckSubBox(int row, int col, int number);
	bool CheckForUnassigned();
    
    virtual ~mySudoku();
private:
    int Data[SIZE][SIZE];
};

#endif	/* MYSUDOKU_H */

解决方案

user ./a.out < input.txt


The problem I am facing is that my solver will solve for some puzzles but for others it returns segmentation fault (core dumped).

I am having trouble understanding what could cause this error.

It can solve these two puzzles

2 0 7 0 9 0 4 0 6     
0 4 0 2 6 7 0 5 3
5 0 0 3 8 4 0 0 0
6 0 0 5 2 8 3 0 7
8 2 5 0 3 1 0 0 9
0 0 1 9 4 6 0 2 0
4 0 2 6 1 9 7 0 8
9 1 3 8 0 0 0 0 4
7 0 0 0 5 3 1 9 2


0 0 5 7 1 0 6 8 2
0 3 6 0 4 0 0 7 5
7 0 0 5 8 6 3 4 9
9 8 4 0 5 7 2 0 0
1 5 0 0 0 0 7 9 8
6 7 2 8 0 0 0 0 4
0 6 8 2 0 9 0 1 0
3 4 9 1 0 0 8 2 0
0 1 7 0 3 0 9 0 6



but for example these two puzzles it will return segmentation fault (core dumped)

3 0 0 2 0 0 0 0 0
0 0 0 1 0 7 0 0 0
7 0 6 0 3 0 5 0 0
0 7 0 0 0 9 0 8 0
9 0 0 0 2 0 0 0 4
0 1 0 8 0 0 0 5 0
0 0 9 0 4 0 3 0 1
0 0 0 7 0 2 0 0 0
0 0 0 0 0 8 0 0 6



0 0 0 0 0 3 0 1 7
0 1 5 0 0 9 0 0 8
0 6 0 0 0 0 0 0 0
1 0 0 0 0 7 0 0 0
0 0 9 0 0 0 2 0 0 
0 0 0 5 0 0 0 0 4
0 0 0 0 0 0 0 2 0
5 0 0 6 0 0 3 4 0 
3 4 0 2 0 0 0 0 0



When using a debugger ( with the puzzles that don't work ) the program stops at my first check method in my solve function and returns the error.

//where the debugger returns the error 
bool mySudoku::CheckRow(int row, int number){ //check row for user inputted number 
    	for(int i=0; i< SIZE; i++)            //return true if number is found
	{                                     //false if not
            if(Data[row][i] == number)
            {
				return true;
            }
	}
    return false;
}



Here is my .cpp file

#include "mySudoku.h"
#include <cstdlib>
#include <iostream> 

using namespace std;

mySudoku::mySudoku(){ //default constructor 
    for(int row=0; row < SIZE; row++)
    {
        for(int col=0; col < SIZE; col++)
        {
            Data[row][col] = 0;
        }
    }
    //initalize Array's data to all 0's
}

mySudoku::mySudoku(int anArray[][SIZE]){ //alternate constructor 
    for(int row=0; row < SIZE; row++)
    {
        for(int col=0; col < SIZE; col++)
        {
            Data[row][col] = anArray[row][col];
        }
    }
}

mySudoku::mySudoku(const mySudoku& orig){ //copy constructor
    
}

mySudoku::~mySudoku(){
    //destructor
}

void mySudoku::ReadPuzzle(){  //read the puzzle 
    cout << "Please input values for grid of the puzzle" <<endl;
    for(int row=0;row< SIZE; row++)
    {
        for(int col=0; col< SIZE; col++)
        {
			cout << "Row " << row+1 << " Col " << col+1 << endl;
            cin >> Data[row][col];
        }
    }
    cout << "you have completed inputting values for the grid!" << endl;
}

void mySudoku::PrintPuzzle(){ //prints the puzzle 
    for(int row=0; row < SIZE; row++)
    {
        for(int col=0; col < SIZE; col++)
        {
            cout << Data[row][col] << " ";
        }
        
        cout << endl;
    }
}

bool mySudoku::CheckCol(int col, int number){  //check col for user inputted number
	for(int i=0;i< SIZE; i++)               //return true if number is found 
	{                                       //false if not 
            if(Data[i][col] == number)
            {
                return true;
            }
	}
    return false;
}

bool mySudoku::CheckRow(int row, int number){ //check row for user inputted number 
    	for(int i=0; i< SIZE; i++)            //return true if number is found
	{                                     //false if not
            if(Data[row][i] == number)
            {
				return true;
            }
	}
    return false;
}

bool mySudoku::CheckSubBox(int row, int col, int number){ //check sub box for user inputted number 
    int startIndexRow = row-row%3;					      //return true if number is found
    int startIndexCol = col-col%3;						  //false if not
    
    for(int i=0; i<3; i++)
    {
        for(int j=0; j<3; j++)
        {
            if(Data[startIndexRow+i][startIndexCol+j] == number)
            {
                return true; 
            }
        }
    }
    return false; 
}

bool mySudoku::CheckForUnassigned() //checks for unassigned values 
{									//which are denoted by a zero 
	for(int i=0; i<SIZE;i++)		//return true if unassigned value exist 
	{								//false if not 
		for(int j=0; j<SIZE; j++)
		{
			if(Data[i][j] == 0)
			{
				return false;
			}
		}
	}
	return true;

}

int mySudoku::SolvePuzzle(int iterations){ //solves the puzzle on two conditions 
int counter=0;							   //by a certain number of iterations 
while(iterations != 0)					   //or until convergence 
{
	for(int i=0; i < SIZE; i++) 
	{
		for(int j=0; j < SIZE; j++) 
		{
			if(Data[i][j]==0)
			{
				int ansCount=0;
				int ans =0;
				for(int k=1; k < SIZE+1; k++)
				{
					if((CheckRow(i,k)==false)&&(CheckCol(j,k)==false)&&(CheckSubBox(i,j,k)==false))
					{
						ansCount++;
						ans = k;
					}
					
				}
				if(ansCount == 1)
				{
					Data[i][j] = ans;
				}
			}

		}
	}
	if(iterations > 0)
	{
		iterations--;
	}
	else if((iterations < 0) && (CheckForUnassigned()==true))
	{
		counter++; 
		return counter; 
	}
	else if((iterations < 0) && (CheckForUnassigned()==false))
	{
		counter++;
		SolvePuzzle(iterations);
	}

	counter++;

}
 return counter;

}



Here is my main file

#include <cstdlib>
#include <iostream>
#include "mySudoku.h"

using namespace std;

/*
 * 
 */
int main(int argc, char** argv) {
    //initialize test array
    int testArray[9][9]; 
    
    //populate array
    for(int row=0; row<9; row++)
    {
        for(int col=0; col<9; col++)
        {
            testArray[row][col] = 0;
        }
    }
    
    
    //define object mySudoku 
    mySudoku Stest1;
    mySudoku Stest2(testArray);
    
    
    Stest1.ReadPuzzle();    //read puzzle 
    Stest1.CheckCol(1,3);  //check column 
    Stest1.CheckRow(1,3);  //check row
    Stest1.CheckSubBox(4,4,5); //check 3x3 subbox
    cout << " iteration: " << Stest1.SolvePuzzle(-1) << endl;  //solves puzzle test 
    Stest1.PrintPuzzle();   // print puzzle 
    cout << "\n";
    Stest2.PrintPuzzle();
   
    return 0;
}




And my .h file

#ifndef MYSUDOKU_H
#define	MYSUDOKU_H

const int SIZE(9); //constant 

class mySudoku {
public:
    mySudoku(); //default constructor 
    mySudoku(int anArray[][SIZE]); //alternate constructor 
    mySudoku(const mySudoku& orig);//copy constructor 

    //functions 
    void ReadPuzzle(); 
    void PrintPuzzle(); 
    int SolvePuzzle(int iterations);
    bool CheckRow(int row, int number);
    bool CheckCol(int col, int number);
    bool CheckSubBox(int row, int col, int number);
	bool CheckForUnassigned();
    
    virtual ~mySudoku();
private:
    int Data[SIZE][SIZE];
};

#endif	/* MYSUDOKU_H */


This exception is raised in most cases when your application goes outside of the segment it was assignment, and tries to overwrite or interact with the memory areas that it does not have access to.

I am not sure where that "SIZE" variable (or constant) comes from or what you are doing with it, be the problem is clearly an invalid access or unauthorized access to a memory location where your program does not have access to propagate. Your program may be accessing i from outside the bounds because of this SIZE variable.

You mentioned, the problem is here:

if(Data[row][i] == number)
{
   return true;
}



Maybe, now do yourself a favor, and instead of going deeper in debugging. Re-read the statements,

if, ith element in rowth array of Data is equal to number, then return true.

Now, program would do the same thing. It would try to capture the ith element, from the rowth array in your Data object (where Data is a multi-dimen array). If that is accessible, then it would compare, otherwise if the memory is protected, is not available to your program, is authorized to another program, kernel would simply raise this error and program would terminate. Anyways, read the following resources to understand the problem and solve it by overcoming the causes. :-)

For more please refer to these threads:

http://stackoverflow.com/questions/19641597/what-is-segmentation-fault-core-dumped[^]
http://ubuntuforums.org/showthread.php?t=2071988[^]
http://stackoverflow.com/questions/2346806/what-is-segmentation-fault[^]

https://en.wikipedia.org/wiki/Segmentation_fault[^]


这篇关于为什么此代码显示分段错误(核心转储)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆