c++ - 为什么我的 C++ 数独检查器出现分段错误?
问题描述
我实际上是在codingame.com 上编码,我的程序中出现了一个对我来说没有意义的分段错误。
我创建了一个 Grid 类,它基本上是一个 (9*9) 字符的二维数组和几个检查函数。
我的 public check() 调用 private checkLine()、checkRow() 和 checkSquare()。这 3 个中的每一个依次调用 checkNine()。
#include <iostream>
#include <string>
#include <vector>
#include <array>
#include <algorithm>
using namespace std;
class Grid
{
public:
Grid(string str)
{
for (int i = 0; i < 81; i++)
{
this->sudoku[i/9][i%9] = str[i];
}
}
string check()
{
bool result = true;
for (int i = 0; i < 9; i++)
{
result = result && checkLine(i) && checkRow(i);
}
return result ? "true" : "false";
}
private:
bool checkLine(int line)
{
cerr << "line:" << line << endl;
vector<char> vec;
for (int j = 0; j < 9; j++)
{
vec.push_back(this->sudoku[line][j]);
}
return checkNine(vec);
}
bool checkRow(int row)
{
cerr << "row:" << row << endl;
vector<char> vec;
for (int k = 0; k < 9; k++)
{
vec.push_back(this->sudoku[k][row]);
}
return checkNine(vec);
}
bool checkNine(vector<char> nine)
{
array<int, 9> tmp;
tmp.fill(0);
for(int m = 0; m < 9; m++)
{
tmp[nine[m - '0']] = tmp[nine[m - '0']] + 1;
}
return true;
}
char sudoku[9][9];
};
int main()
{
string str;
str = "123456789456789123789123456912345678345678912678912345891234567234567891567891234";
Grid grid(str);
cout << grid.check() << endl;
}
如您所见,我放置了 cerr 语句来尝试查看发生了什么。
当我尝试运行该程序时,这就是我得到的:
Erreurs
Segmentation fault.
at new_allocator.h. function __gnu_cxx::new_allocator<char>::deallocate (this=0x7fffffffe8d0, __p=0x55565556ef50 <error: Cannot access memory at address 0x55565556ef50>) on line 128
at alloc_traits.h. function std::allocator_traits<std::allocator<char> >::deallocate (__a=..., __p=0x55565556ef50 <error: Cannot access memory at address 0x55565556ef50>, __n=18446744069414584329) on line 470
at stl_vector.h. function std::_Vector_base<char, std::allocator<char> >::_M_deallocate (this=0x7fffffffe8d0, __p=0x55565556ef50 <error: Cannot access memory at address 0x55565556ef50>, __n=18446744069414584329) on line 351
at stl_vector.h. function std::_Vector_base<char, std::allocator<char> >::~_Vector_base (this=0x7fffffffe8d0, __in_chrg=<optimized out>) on line 332
at stl_vector.h. function std::vector<char, std::allocator<char> >::~vector (this=0x7fffffffe8d0, __in_chrg=<optimized out>) on line 680
at Answer.cpp. function Grid::checkLine (this=0x7fffffffe950, line=0) on line 37
at Answer.cpp. function Grid::check[abi:cxx11]() (this=0x7fffffffe950) on line 24
at Answer.cpp. function main () on line 94
Sortie standard :
line:0
row:0
其中第 37 行是“vec.push_back(this->sudoku[line][j]);”
谁能把我送到正确的方向?
编辑:旁注,我的 checkNine() 没有完成,它总是返回 true,但这不是重点。
解决方案
两个错误:
bool checkNine(vector<char> nine)
{
array<int, 9> tmp; // <-- Too small, it has to hold 10 digits
tmp.fill(0);
for (int i = 0; i < 9; i++)
{
tmp[nine[i]] = tmp[nine[i]] + 1; // <-- Out of bounds access
}
return true;
}
nine
向量保存char
值。这些char
值是数字的字符表示。但是,该tmp
数组假定int
数字的版本,而不是char
类型。
简而言之,您(例如)假设'2' == 2
, 而事实并非如此。
因此,您需要将其转换char
为int
版本:
tmp[nine[i] - '0'] = tmp[nine[i] -'0'] + 1;
另一个错误是tmp
必须保存 10 位,因此数组太小。它的大小应该是10
,而不是9
。
array<int, 10> tmp;
推荐阅读
- r - 找到二元因变量的序数预测变量的相对重要性
- python - 用零替换列表中的负值
- html - Tkinter 标签中的 HTML Dec 代码图像 - 文本或图像加倍
- c++ - 如何在 C++ 中添加 getline?
- python - 不和谐.py | 给特定用户特定角色的简单命令?
- mongodb - MongoDB 合并查询作为聚合管道
- azure-devops - 已解决 - Azure DevOps“用户无权访问添加到此管道的变量组,或者找不到它们。ID:3”
- javascript - Vue 3.0 使用 Var 作为 Key
- dataframe - 将多个函数应用于 Julia DataFrames.jl 中的单个列
- flutter - 一个类在被释放后被使用。一旦你在一个类上调用了 dispose() ,它就不能再使用了