简体   繁体   English

B迷宫C ++分段错误错误

[英]BFS for Maze C++ Segmentation Fault error

I am trying to create a BFS for a maze that will stop when a certain point is reached. 我正在尝试为迷宫创建BFS,该迷宫将在到达特定点时停止。 While testing it I'm running into a Segmentation fault (core dumped) error. 在测试时,我遇到了分段错误(核心转储)错误。 I am trying to modify the code I found on this site . 我正在尝试修改在此站点上找到的代码。 The main differences between what I am trying to do and the code in that site are that I do not need to output the distance traveled so and I am also supposed to output the order in the queue the vertices are in the inside of the matrix. 我要执行的操作与该站点中的代码之间的主要区别是,我不需要输出行进的距离,因此我也应该在顶点在矩阵内部的队列中输出顺序。 For example, the output should be the following: 例如,输出应为以下内容:

What the output of the program should be 程序的输出应该是什么

The Hashes represent vertices that do not get added to the queue I am not too concerned with them as of now due to this error that is stopping me from going forward. 哈希表示尚未添加到队列中的顶点,由于此错误使我无法继续前进,到目前为止,我对此不太担心。

Here is my code thus far: 到目前为止,这是我的代码:

#include <queue> 
#include <iostream>
#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define ROW 8
#define COL 11

struct Point{
    int x;
    int y;
};


bool isValid(int x, int y) 
{ 
    // return true if row number and column number 
    // is in range 
    return (x >= 0) && (x< ROW) && 
           (y >= 0) && (y < COL); 
} 

int rowNum[] = {-1, 0, 0, 1}; 
int colNum[] = {0, -1, 1, 0}; 

int BFS(int mat[][COL], Point src, Point dest) 
{ 

    int order = 1;
    // Mark the source cell as visited

   bool visited[ROW][COL];

   memset(visited, false, sizeof visited); 


   visited[src.x][src.y] = true;


    // Create a queue for BFS 
  queue <Point> vertices;

  // Enqueue source cell 

  vertices.push(src);
    // Do a BFS starting from source cell

    while(visited[dest.x][dest.y] != true){

        Point current = vertices.front();

        vertices.pop();

        for (int i = 0; i < 4; i++) 
        { 

            int row = current.x + rowNum[i]; 
            int col = current.y + colNum[i]; 

            // if adjacent cell is valid, has path and 
            // not visited yet, enqueue it. 

            if (isValid(row, col) == true && (mat[row][col]) &&  
               !visited[row][col]) 
            { 
                cout << "Hi" << endl;
                // mark cell as visited and enqueue it 
                visited[row][col] = true;
                mat[row][col] = order;
                order++;
                vertices.push(current);
                cout << vertices.front().x;
                cout << vertices.front().y;
            } 
            else {

            }
        } 
    }

     return -1;
} 

int main() 
{ 

    int mat[ROW][COL] = 
    { 
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 


    }; 

    Point source = {0, 0}; 
    Point dest = {3, 4}; 

    BFS(mat, source, dest);

    for (int i = 0; i < ROW; ++i)
    {
        for (int j = 0; j < COL; ++j)
        {
            std::cout << mat[i][j] << ' ';
        }
        std::cout << std::endl;
    }

    return 0; 
}

I did some troubleshooting beforehand and have found that the error most likely is caused by this spot right here: 我事先做了一些故障排除,发现错误很可能是由此位置引起的:

if (isValid(row, col) == true && (mat[row][col]) &&  
               !visited[row][col]) 
            { 

                // mark cell as visited and enqueue it 
                visited[row][col] = true;
                mat[row][col] = order;
                order++;
                vertices.push(current);
                cout << vertices.front().x;
                cout << vertices.front().y;
            } 

I assume that because I set up a bunch of output messages (you may notice the cout "Hi's") to occur at certain points in the cause to find the source of the error and that is where it led me. 我以为是因为我设置了一堆输出消息(您可能会注意到提示“ Hi's”)会在导致错误原因的某些位置发生,这就是导致错误的原因。

Any help is appreciated and just to be clear i am trying to find out why i am getting the segmentation fault error. 任何帮助表示赞赏,只是要明确我试图找出为什么我得到细分错误。

Thank you. 谢谢。

Ah, one of the best errors in programming, the dreaded segmentation fault ! 啊,编程中最好的错误之一,可怕的segmentation fault Logging to stdout is one way to debug, but you can make your own life a lot easier by using a debugger (eg gdb ). 登录到stdout是一种调试方式,但是您可以使用调试器(例如gdb )使自己的生活变得更加轻松。

Let's say your executable is called bfs , and it produced a core dump called core.bfs.12345 ( 12345 was the process's PID). 假设您的可执行文件名为bfs ,它产生了一个名为core.bfs.12345的核心转储( 12345是进程的PID)。 Invoke gdb like this: 像这样调用gdb

$ gdb ./bfs core.bfs.12345

Immediately upon entering, gdb will tell you where your program crashed—no print statements needed! 进入后, gdb会立即告诉您程序在哪里崩溃了-无需打印语句! Here's what it should look like: 它应该是这样的:

Program terminated with signal 11, Segmentation fault.
#0  0x0000000000400d3e in BFS (mat=0x7fffffffd950, src=..., dest=...) at ../bfs/bfs.cxx:54
54              Point current = vertices.front();

Essentially, it's telling you to look at line 54 (your line number may be different) where you've called queue<Point>::front() . 从本质上讲,它告诉您看一下第54行(您的行号可能不同),在这里您将queue<Point>::front()调用了。 But, if a queue is empty, calling front() is undefined behavior . 但是,如果队列为空,则调用front()未定义的behavior

It seems like the rest of your code is pretty much on track. 看起来您的其余代码几乎步入正轨。 I'd encourage you to rethink the while loop condition. 我鼓励您重新考虑while循环条件。 Perhaps, there is a 'safer' way to know whether or not to keep searching. 也许,有一种“更安全”的方式来知道是否继续搜索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM