簡體   English   中英

為什么我的 C++ 數獨檢查器出現分段錯誤?

[英]Why do I get a segmentation fault in my C++ sudoku checker?

我實際上是在 codingame.com 上編碼,我的程序中出現了對我來說沒有意義的分段錯誤。

我創建了一個網格 class,它基本上是一個 (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;  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM