c++ - 被信号 11(SIGSEGV) 和/或 6(SIGABRT) 杀死

标签 c++ shortest-path sigabrt segmentation-fault maze

<分区>

我的程序有这个小问题。在 Visual Studio 2012 中它运行良好,但如果我用 G++ 编译它(是的,由于上述原因,我必须使用它来编译),根据输入触发错误信号 11(SIGSEGV) 或 6(SIGABRT) .这是一个编程练习,我有另一个程序(在在线服务器上)用 10 个不同的输入测试我的程序。正如我所说,该程序在使用 Visual Studio 2012 时编译并运行良好。

关于项目: 它找到从起点 (x,y) 到多个导出的最短路径(导出的数量无关紧要且不同。可能只有 1 个导出,也可能有 200 个)。 输入如下:

7 12          // maze height and width
##########.#  //
#..........#  //
#.###.######  //
#..X#.#.....  // the maze blueprint
#.###.#.####  //
#..........#  //
############  //

还有我的程序:

#include <iostream>
#include <vector>

typedef struct _laby_t {
    int h, w;
    char **pohja; // 'pohja' is finnish and means layout
} laby_t;

typedef std::vector<int> monovector;
typedef std::vector< std::vector<int> > bivector;

laby_t *laby_allocate (int r, int c)
{
    laby_t *laby;
    int i;

    laby = new laby_t[sizeof (laby_t)];
    laby->pohja = new char *[r];
    for (i = 0; i < r; i++)
    {
        laby->pohja[i] = new char[c];
    }
    laby->h = r;
    laby->w = c;

    return laby;
}

int wander(int y, int x, laby_t *&_laby, int goals)
{
    laby_t *laby = _laby;
    int found = 0, depth = 0, min_path = 1000000;
    bool b = 0;
    bivector openList;
    monovector start; start.push_back(y); start.push_back(x);
    bivector closedList;

    openList.push_back(start);

    while(found < goals)
    {

        y = openList.back()[0]; x = openList.back()[1];
        monovector r; r.push_back(y); r.push_back(x); closedList.push_back(r);
        openList.pop_back();
        if(laby->pohja[y][x] != '*') laby->pohja[y][x] = '-';
        depth++;

        if(y == 0 || y+1 == laby->h || x == 0 || x+1 == laby->w) {
            found++;
            if(depth < min_path) min_path = depth;
            if(found >= goals) {
                std::cout << min_path << std::endl;
                break;
            }
            laby->pohja[y][x] = '-';

            goto back_track;
        }
        else
        {
            b = 0;
            if(laby->pohja[y+1][x  ] == '.') { monovector r; r.push_back(y+1); r.push_back(x); openList.push_back(r); b=1; }
            if(laby->pohja[y  ][x+1] == '.') { monovector r; r.push_back(y); r.push_back(x+1); openList.push_back(r); b=1; }
            if(laby->pohja[y-1][x  ] == '.') { monovector r; r.push_back(y-1); r.push_back(x); openList.push_back(r); b=1; }
            if(laby->pohja[y  ][x-1] == '.') { monovector r; r.push_back(y); r.push_back(x-1); openList.push_back(r); b=1; }
            if(!b)
            {
back_track:     while(closedList.size() > 0)
                {
                    //std::cout << closedList.size() << std::endl;
                    int c_y = closedList.back()[0]; int c_x = closedList.back()[1];
                    int o_y = openList.back()[0];   int o_x = openList.back()[1];

                    laby->pohja[y][x] = '*';

                    y = c_y; x = c_x;

                    laby->pohja[y][x] = '*';

                    if( (c_y+1 == o_y && c_x   == o_x) ||
                        (c_y   == o_y && c_x+1 == o_x) ||
                        (c_y-1 == o_y && c_x   == o_x) ||
                        (c_y   == o_y && c_x-1 == o_x) )
                    {
                        laby->pohja[y][x] = '-';
                        y = o_y; x = o_x;
                        closedList.pop_back();
                        depth--;
                        break;
                    }
                    else {
                        closedList.pop_back();
                        depth--;
                    }
                }
            }
        }
    }

    return min_path;
}

int main()
{
    int h, w, goals = 0;
    std::cin >> h >> w;

    laby_t *laby;
    laby = laby_allocate(h, w);

    for(int i = 0; i < laby->h; i++)
        std::cin >> laby->pohja[i];

    for(int i = 1; i < laby->h-1; i++) {
        if(laby->pohja[i][0] == '.') goals++;
        if(laby->pohja[i][laby->w-1] == '.') goals++;
    }

    for(int i = 1; i < laby->w-1; i++) {
        if(laby->pohja[0][i] == '.') goals++;
        if(laby->pohja[laby->h-1][i] == '.') goals++;
    }

    for(int i = 0; i < laby->h; i++)
        for(int j = 0; j < laby->w; j++) {
            if(laby->pohja[i][j] == 'X') {
                wander(i, j, laby, goals);
                goto _exit;
            }
        }

_exit:

    //system("pause");
    return 0;
}

我已经完成了关于错误信号的作业,以防你们不知道:http://www.yolinux.com/TUTORIALS/C++Signals.html

提前致谢。

最佳答案

代码可以在带有 G++ 4.7.1 的 Mac OS X 10.7.5 上干净地编译,这很好:

g++ -g -Wall -Wextra laby.cpp -o laby 

不幸的是,当结果在 valgrind 下运行时,它产生:

==15030== Invalid write of size 1
==15030==    at 0x306BE: std::basic_istream<char, std::char_traits<char> >& std::operator>><char, std::char_traits<char> >(std::basic_istream<char, std::char_traits<char> >&, char*) (in /usr/lib/libstdc++.6.0.9.dylib)
==15030==    by 0x10000117D: main (laby.cpp:113)
==15030==  Address 0x10001632c is 0 bytes after a block of size 12 alloc'd
==15030==    at 0xB823: malloc (vg_replace_malloc.c:266)
==15030==    by 0x5768D: operator new(unsigned long) (in /usr/lib/libstdc++.6.0.9.dylib)
==15030==    by 0x576DA: operator new[](unsigned long) (in /usr/lib/libstdc++.6.0.9.dylib)
==15030==    by 0x1000008C0: laby_allocate(int, int) (laby.cpp:21)
==15030==    by 0x100001146: main (laby.cpp:110)

因此,laby_allocate() 函数中的内存分配存在问题。或者几个...

laby_t *laby_allocate (int r, int c)
{
    laby_t *laby;
    int i;

    laby = new laby_t[sizeof (laby_t)];

这一行分配了一个laby_t的数组;它在数组中分配与 laby_t 中的字节数一样多的元素。这不是您需要的。

    laby = new laby_t;

继续:

    laby->pohja = new char *[r];
    for (i = 0; i < r; i++)
    {
        laby->pohja[i] = new char[c];
    }

这没有为数据末尾的 null 分配足够的空间……这就是为什么“写入”是 1 个字节的原因。将 c 更改为 c+1 并且 valgrind 给出了干净的健康状况。

    laby->h = r;
    laby->w = c;

    return laby;
}

给出的答案是15;我不相信这是正确的。

关于c++ - 被信号 11(SIGSEGV) 和/或 6(SIGABRT) 杀死,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14083839/

相关文章:

c# - Eigen 点到平面算法,返回四元数?

c++ - 什么是 std::views::counted?

c++ - 具有类类型非类型模板参数的类模板成员的类外定义

java - Dijkstra 算法寻找所有可能的最短路径

python - networkx,摩尔邻域,最短路径问题

swift - SIGABRT 错误 swift

iphone - iPhone SIGABRT崩溃0x00000000:帮助我进行多项选择

c++ - 如何检查文件是否仍在写入?

python - 从 OSMnx 为 NetworKX 生成加权图

swift - 如何重新连接 IB socket