"Stack overflow"错误。指针可能不在列表中
"Stack overflow" error. Probably pointer out of list
首先,我不精通编程,所以请宽容。:)
我很好奇是什么原因导致了"堆栈溢出"的错误。我正在使用Visual C++2010学习版。
struct elem
{
BITMAP * colltile;
elem * next;
};
/*在此处输入一些代码*/
int collision_map (unsigned int poz_x, unsigned int poz_y)
{
elem * wsk = this->where_the_head_of_list_is;
int x,y;
x = poz_x%64; //coord x on tile (0-63px)
y = poz_y%64; //coord y on tile (0-63px)
poz_x /= 64; //preparing poz_x and poz_y to point on a tile on a grid
poz_y /= 64; //integers do not have to be floored
//for (int j=(poz_y*(this->size_x)+poz_x); j>0; j--) //normally works... but
for (int j=0; j<1000; j++) //this version is not
{
if ((!(wsk = wsk->next)) ||
((poz_x+1) > this->size_x) ||
((poz_y+1) > this->size_y))
{ //should check if there is no new pointer or just out of map
return -1;
}
}
return getpixel(wsk->colltile, x, y);
}
当j
达到列表长度值时,为什么条件不起作用?
您可以/必须做的事情:
- 添加调试输出将帮助您找到错误。在代码中添加一些
std::cerr << wsk << std::endl
,并检查它是否/何时变为零 - 指针可能在
elem* wsk= ...
函数的开头为零。如果是这种情况,wsk->next
将访问一个空指针。您必须检查指针是否有效 - 由于类中有指针成员(如
this->where_the_head_of_list_is
所示),因此需要实现复制构造函数、赋值运算符和析构函数,而您可能没有实现这些。有关解释,请参见此处
相关文章:
- 为什么这个 std::queue/指向结构的指针列表直到 List.Size() == 0 才释放内存?
- 创建一个棋盘格或"Interweave"两个链接列表。IE 更改两个链表的指针
- 如何在构建链接列表时调整头、尾指针
- 什么更好?返回对象指针列表?或返回指向对象列表的指针?
- 处理从列表中删除指向对象的指针的"healthy"方法是什么?
- 编译包含指向模板函数的指针的初始值设定项列表时,gcc 出错,但 clang 不出错
- 如果函数采用指向类的指针,则函数将完全脱离候选列表
- 如何将共享指针列表传递到pybind11中的c++对象
- Unique_ptr:在列表中放置时未分配释放的指针
- C++:如何初始化包含具有给定键的指针列表的映射
- 使用继承的指针列表复制构造函数或重载运算符=
- 什么是结构开头的函数列表指针,称为 c++
- 使用 c++ 的 STL 列表 ..在传递列表指针的数组中
- 静止图像::获取设备列表指针
- 列表指针类的输出向量
- 使用PThreads访问全局列表-指针问题
- 操作迭代器列表+指针
- 在c++中分配带有列表指针值的STD映射
- python链接列表指针和大小
- 如何调整STL列表指针的大小