被信号11(SIGSEGV)和/或6(SIGABRT)杀死
Killed by signal 11(SIGSEGV) and/or 6(SIGABRT)
我的程序有这个小问题。在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
;我不相信那是正确的。
这一行溢出了您的内存分配。如果用户输入w字符,则需要(w+1)字符来保存以空结尾的字符串。
std::cin >> laby->pohja[i];
这一行也分配了一个包含许多laby_t
对象的数组,尽管您似乎只想要一个。也许你把c++ new
和C malloc
搞混了。
laby = new laby_t[sizeof (laby_t)];
你可以用这个代替它。
laby = new laby_t;
这似乎也是c语言的残余。这不是一个bug,但它不必要地用多余的符号污染了当前的命名空间。
typedef struct _laby_t { ... } laby_t;
你可以用这个代替它。
struct laby_t { ... };
- 中止信号来自 C++ 中的中止(3) (SIGABRT)
- 为什么我在此代码中收到 SIGABRT 错误
- 父进程意外被子 cmd 杀死.exe运行 java jar 的进程 - WINDOWS 7 问题 - C++
- OpenCL 内核计时测量 0 秒或导致 SIGABRT
- 如何在不丢失输出的情况下杀死/停止终端中正在运行的进程?
- 优化正在杀死我在 clang 6 中的整数溢出检查
- 该程序没有产生输出,而是卡在一个循环中,几秒钟后消失并打印被杀死?
- Android因为权限问题而杀死Qt应用程序
- C++中链表的错误"Abort signal from abort(3) (sigabrt) "
- SIGABRT 和线程相关的异常,但在调试期间工作正常
- 如何杀死被条件变量锁定的线程?
- C++ 杀死一个标准::线程
- 当同一个程序被打开两次时,如何使用C ++杀死一个程序?
- SIGABRT 在线程中访问内存时
- 使用 ctrl + C 在 python 中杀死C++进程
- EXC_BAD_ACCESS或SIGABRT,同时从C++拨打scipy.stats.anderson_ksamp
- 杀死幻数:"const int" vs "constexpr int"(或者最后没有区别)
- C 从子线中杀死所有父母的孩子
- 当我的代码工作正常时,为什么在SPOJ中遇到运行时(Sigabrt)错误
- 被信号11(SIGSEGV)和/或6(SIGABRT)杀死