在栈中推入时出现分段错误
Getting segmentation fault while pushing in stack
我正在尝试使用c++中可用的堆栈模板为DFS编写一个简单的代码。
在运行时,core dump会出现分段错误。
我的代码片段如下:
#define XPIX 400
#define YPIX 600
这是节点类的定义:
class node{
public:
int x;
int y;
node(int x,int y);
};
node::node(int x, int y){
this->x = x;
this->y = y;
}
主类如下:
class grafixMask {
public:
vector <int> sortedAreas(vector <string> rectangles);
private:
bool bitmap[XPIX][YPIX];
stack <node> st;
vector <int> parseInput(vector <string> Input);
void init(int xLeft, int yLeft, int xRight, int yRight);
int depth_first_search(int x,int y);
};
DFS的功能如下:
int grafixMask::depth_first_search(int x, int y){
int result = 0;
this->st.push(node(x,y));
while(this->st.empty() == false){
node topEle = this->st.top();
this->st.pop();
if(topEle.x < 0 || topEle.x > XPIX) continue;
if(topEle.y < 0 || topEle.y > YPIX) continue;
if(this->bitmap[topEle.x][topEle.y] == true) continue;
this->bitmap[topEle.x][topEle.y] = true;
result++;
this->st.push(node(topEle.x - 1,topEle.y));
this->st.push(node(topEle.x,topEle.y + 1));
this->st.push(node(topEle.x + 1,topEle.y));
this->st.push(node(topEle.x,topEle.y - 1));
}
return result;
}
我分析了核心转储,它说:
#1 0x0000000000403b47 in std::allocator<node>::~allocator ( this=0x7fffea214780, __in_chrg=<optimized out>) at /usr/include/c++/4.7/bits/allocator.h:112
#2 0x0000000000402d1b in std::_Deque_base<node, std::allocator<node> >::~_Deque_base (this=0x6dd3c0, __in_chrg=<optimized out>) at /usr/include/c++/4.7/bits/stl_deque.h:568
#3 0x000000000040216e in std::vector<std::string, std::allocator<std::string> >::push_back (this=0x7fffea24f340, __x=...) at /usr/include/c++/4.7/bits/stl_vector.h:887
#4 0x00000000004016f6 in grafixMask::depth_first_search ( this=0x7fffea2149c0, x=0, y=0) at B.cpp:137
#5 0x00000000004012c7 in grafixMask::sortedAreas (this=0x7fffea2149c0, rectangles=...) at B.cpp:87 #6 0x0000000000401965 in main () at B.cpp:154
调试显示问题出在第134行:
(gdb) print topEle.y
$5 = 51
(gdb) next
132 this->bitmap[topEle.x][topEle.y] = true;
(gdb) next
133 result++;
(gdb) next
134 this->st.push(node(topEle.x - 1,topEle.y));
(gdb) next
Program received signal SIGSEGV, Segmentation fault.
0x00000000004038b9 in __gnu_cxx::new_allocator<node>::construct (
this=0x7fffffffde50, __p=0x160d698, __val=...)
at /usr/include/c++/4.7/ext/new_allocator.h:120
120 { ::new((void *)__p) _Tp(__val); }
根据源代码,这些语句位于第134至137行之间。
this->st.push(node(topEle.x - 1,topEle.y));
this->st.push(node(topEle.x,topEle.y + 1));
this->st.push(node(topEle.x + 1,topEle.y));
this->st.push(node(topEle.x,topEle.y - 1));
请说明stack
中node
元素push失败的原因
这一行好像有问题
if(topEle.x < 0 || topEle.x > XPIX) continue;
你已经声明你的数组为bool bitmap[XPIX][YPIX];
,这意味着索引范围为0…XPIX-1是允许的。将topEle.x > XPIX
替换为topEle.x >= XPIX
你声明了一个数组:
bool bitmap[XPIX][YPIX];
而在你的代码:
if(topEle.x < 0 || topEle.x > XPIX) continue;
if(topEle.y < 0 || topEle.y > YPIX) continue;
if(this->bitmap[topEle.x][topEle.y] == true) continue;
this->bitmap[topEle.x][topEle.y] = true;
如果topEle.x
== XPIX或topEle.y == YPIX
,代码this->bitmap[topEle.x][topEle.y]
确实损坏了内存,并且在下一次内存分配或释放时,错误将会出现
相关文章:
- 在某些循环内使用vector.push_back时出现分段错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 当我的阵列太大时出现分段错误
- 分段错误当我试图运行程序时出错
- 在c++中初始化矩阵时出现分段错误(核心转储)
- 尝试使用集合函数时出现分段错误
- 我无法缩小此分段错误的原因
- g++的分段错误(在NaN上使用to_string两次时)
- 我是如何在这段代码中出现分段错误的
- 创建结构的数组时遇到分段错误
- 在c++中键入向量中的所有值后,得到分段错误(核心转储)
- 在 c++ 中实现 Trie 时出现分段错误
- 为什么 fstream 在打开带有格式的文件时会导致分段错误?
- 为什么我遇到分段错误?
- 动态类的分段错误(家庭作业问题)
- 分段错误 - 读取初始化指针的数组
- 如何摆脱C ++中的分段错误错误?
- 使用 CTYPE 时出现分段错误
- 为什么代码给出分段错误?