段故障向量<向量<列表<对象*> > >推回

发布于 2024-09-11 14:24:27 字数 1588 浏览 5 评论 0 原文

我有一个像这样定义的 3D 矢量...

std::vector<std::vector<std::list<Object*> > > m_objectTiles;

我有这个代码...

void ObjectManager::AddObject( Object *object ) {
  m_objects.push_back( object );
  m_objectTypes.insert( std::make_pair(
    ObjectAttorney::GetType( object ), object )); 

  int x = ObjectAttorney::GetTileX( object );
  int y = ObjectAttorney::GetTileY( object );
  m_objectTiles[x][y].push_back( object ); // SEG FAULT HERE
}

在 std::vector 中收到此错误 0x0805ccdb >、std::allocator > > >::operator[] ( this=0x8157758, object=0x8173f30) 位于 /usr/include/c++/4.4/bits/stl_vector.h:611 { return *(this->_M_impl._M_start + __n); 我将

其更改为此来测试它...

void ObjectManager::AddObject( Object *object ) {
  m_objects.push_back( object );
  m_objectTypes.insert( std::make_pair(
    ObjectAttorney::GetType( object ), object )); 

  int x = ObjectAttorney::GetTileX( object );
  int y = ObjectAttorney::GetTileY( object );
  std::list<Object*> *l = &m_objectTiles[x][y];
  if ( l ) { // SEG FAULT HERE
    l->push_back( object );
  } else {
    std::cout << "List null.\n";
  }
}

这只是给出一条错误消息,说明在 ObjectManager.cpp:381 发生段错误的位置 ObjectManager::AddObject (this=0x81577a0, object=0x8165760) if ( l ) {

为什么测试空指针时会出现段错误?显然,运算符 [] 返回的内容已损坏或无效。不确定这里出了什么问题。任何帮助表示赞赏。谢谢。

I have a 3D vector defined like this...

std::vector<std::vector<std::list<Object*> > > m_objectTiles;

I have this code...

void ObjectManager::AddObject( Object *object ) {
  m_objects.push_back( object );
  m_objectTypes.insert( std::make_pair(
    ObjectAttorney::GetType( object ), object )); 

  int x = ObjectAttorney::GetTileX( object );
  int y = ObjectAttorney::GetTileY( object );
  m_objectTiles[x][y].push_back( object ); // SEG FAULT HERE
}

that receives this error 0x0805ccdb in std::vector<std::list<Object*, std::allocator<Object*> >, std::allocator<std::list<Object*, std::allocator<Object*> > > >::operator[] ( this=0x8157758, object=0x8173f30) at /usr/include/c++/4.4/bits/stl_vector.h:611 { return *(this->_M_impl._M_start + __n); }

I changed it to this to test it...

void ObjectManager::AddObject( Object *object ) {
  m_objects.push_back( object );
  m_objectTypes.insert( std::make_pair(
    ObjectAttorney::GetType( object ), object )); 

  int x = ObjectAttorney::GetTileX( object );
  int y = ObjectAttorney::GetTileY( object );
  std::list<Object*> *l = &m_objectTiles[x][y];
  if ( l ) { // SEG FAULT HERE
    l->push_back( object );
  } else {
    std::cout << "List null.\n";
  }
}

which just gives an error message saying where the seg fault occured ObjectManager::AddObject (this=0x81577a0, object=0x8165760) at ObjectManager.cpp:381 if ( l ) {

Why would a seg fault occur when testing for a null pointer? Obviously operator [] is returning something corrupted or invalid. Not sure what the problem is here. Any help is appreciated. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

盗梦空间 2024-09-18 14:24:27

出于性能原因,std::vector[] 不进行范围检查。显然,如果 xy 超出范围,则第二种变体没有帮助。

添加这样的检查:

m_objectTiles.size() < x && m_objectTiles[x].size() < y

很难从引用的代码中判断,但可能您希望 std::vector 自动增长。不会的。为此,您需要类似的内容:

m_objectTiles.resize(x);
m_objectTiles[x].resize(y);

在访问 m_objectTiles[x][y] 之前。

std::vector's [] for performance reasons doesn't do range checks. Obviously that second variant doesn't help if x or y are out of range.

Add a check like that:

m_objectTiles.size() < x && m_objectTiles[x].size() < y

It is hard to judge from the quoted code, but it might be that you want std::vector to grow automatically. It will not. For that you would need something like that:

m_objectTiles.resize(x);
m_objectTiles[x].resize(y);

before accessing the m_objectTiles[x][y].

冰火雁神 2024-09-18 14:24:27

您的问题最可能的原因是 ObjectAttorney::GetTileX 和 ObjectAttorney::GetTileY 返回超出范围的值,您检查过它们吗?

崩溃转储将 if 语句指示为崩溃站点的原因可能是旧的调试数据,只需重建您的项目即可。

The most probable cause for your problem is that ObjectAttorney::GetTileX and ObjectAttorney::GetTileY are returning out-of-range values, have you checked them?

The reason for the crash dump to indicate the if statement as the crash site is probably old debugging data, just rebuild your project.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文