为什么设置迭代器指针会导致分段错误?

发布于 2024-12-05 20:14:19 字数 2599 浏览 1 评论 0 原文

最重要的是,为什么迭代器会导致分段错误? 请注意,我在这里键入代码的相关部分,而不是复制粘贴。如果编译有错误,请注意,但不要认为这是我的问题的根源。

在我的代码中,我有一张珠子盒子的地图:
map,set; > > > nextBoxes;

除其他外,每个珠子都有一个 vec pos 成员,该成员又具有:

  class vec{
  double x;
  double y;
  double z;
  .
  .
  .
  vector<int> getBox();
  .
  .
  .
}

vector<int> vec::getBox(){
  vector<int> out(3,0);
  extern double boxSize;
  out[0] = x/boxSize;
  out[1] = y/boxSize;
  out[2] = z/boxSize;
  return out;
}

我使用一种非常简单的方法将珠子穿过到盒子中

extern vectrot<shared_ptr<bead> > beads;
for (int i = 0; i < beads.size(); i++){
  vector<int> t = beads[i]->pos.getBox();
  nextBoxes[t].insert(beads[i]);
}

在代码的其他地方,珠子的 pos 值可能会发生变化,我会在本地更新这些框。

我使用以下循环来遍历盒子和珠子:

map<vector<int>,set<shared_ptr<bead> > >::iterator mit = nextBoxes.begin();
extarn vector<vector<int> >::targets; //holds a vector of a 1, -1 and 0 vectors of distance from the box
extern int boxNum;
while (mit != nextBoxes.end()){
  vector<int> t1 = mit->second();
  set<shared_ptr<bead> >::iterator bit1 = mit->second.begin();
  set<shared_ptr<bead> >::iterator bit2;
  while (bit1 != mit->second.end()){ 
    shared_ptr<bead> b = *bit++;
    vector<int> t2(3,0);
    for (int i = 0; i < 13 i++){//13 is the size of the targets index.
      for (int j = 0; j < 3; j++){
        t2[j] = t1[j] + targets[i][j];
        if (t2[j] >= boxNum) t2[j] -= boxNum;
        if (t2[j] < 0 ) t2[j] += boxNum;
      }
      bit2 = beadsBoxes[t2].begin();
      while (bit2 != nextBoxes[t2].end()){
        shared_ptr<bead> b2 = *bit2++//the segmentation fault is here
        .
        .
        .
      }
    }
  }
}

由于某种原因,我遇到了分段错误。到目前为止我得到的是:

  • 分段错误是由于迭代器的增量引起的。
  • 盒子大小是 2。
  • 这不是我测试的第一个盒子(没有计算它们,但它运行了一段时间)。
  • 我完全不知道如何访问框中的第二个元素(我可以使用迭代器的第一个元素,因为我不增加它。
  • 这是 gdb 的输出错误:

    0 0x00002aaaaab43c65 in std::_Rb_tree_increment(std::_Rb_tree_node_base*) () 来自 /usr/lib64/libstdc++.so.6 1 0x000000000041a28e 位于 /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_tree 的运算符 ++ 中(this=0x63d7c0) .h:265

我将不胜感激任何帮助,谢谢。

编辑: 解决了,有点。

为了将错误减少到最小运行示例,我删除了所有这些行并重写了它们,现在看起来很好。不过,我仍然对这样的案例感兴趣:

如果您愿意,我可以重新问这个问题: 迭代器增量何时会导致 C++ 中的分段错误?

Bottom line, why would iterator cause segmentation fault?
Note, I'm typing here the relevant parts of my code and not copy pasting. If there is an error in compilation, please note but do not consider it the source of my issue.

In my code, I have a map of boxes for beads:
map<vector<int>,set<shared_ptr<bead> > > > nextBoxes;

Each bead has, among others, a vec pos member which, in turn has:

  class vec{
  double x;
  double y;
  double z;
  .
  .
  .
  vector<int> getBox();
  .
  .
  .
}

vector<int> vec::getBox(){
  vector<int> out(3,0);
  extern double boxSize;
  out[0] = x/boxSize;
  out[1] = y/boxSize;
  out[2] = z/boxSize;
  return out;
}

I use a very simple method to get through the beads into the boxes

extern vectrot<shared_ptr<bead> > beads;
for (int i = 0; i < beads.size(); i++){
  vector<int> t = beads[i]->pos.getBox();
  nextBoxes[t].insert(beads[i]);
}

Elsewhere in the code, the pos values of the beads might change and I update the boxes locally.

I use the following loop to go through the boxes and beads:

map<vector<int>,set<shared_ptr<bead> > >::iterator mit = nextBoxes.begin();
extarn vector<vector<int> >::targets; //holds a vector of a 1, -1 and 0 vectors of distance from the box
extern int boxNum;
while (mit != nextBoxes.end()){
  vector<int> t1 = mit->second();
  set<shared_ptr<bead> >::iterator bit1 = mit->second.begin();
  set<shared_ptr<bead> >::iterator bit2;
  while (bit1 != mit->second.end()){ 
    shared_ptr<bead> b = *bit++;
    vector<int> t2(3,0);
    for (int i = 0; i < 13 i++){//13 is the size of the targets index.
      for (int j = 0; j < 3; j++){
        t2[j] = t1[j] + targets[i][j];
        if (t2[j] >= boxNum) t2[j] -= boxNum;
        if (t2[j] < 0 ) t2[j] += boxNum;
      }
      bit2 = beadsBoxes[t2].begin();
      while (bit2 != nextBoxes[t2].end()){
        shared_ptr<bead> b2 = *bit2++//the segmentation fault is here
        .
        .
        .
      }
    }
  }
}

For some reason, I get a segmentation fault. What I got so far is:

  • the segmentation fault is cause because of the incrementation of the interator.
  • the box size is 2.
  • this is not the first box I am testing (didn't count them but it runs for a while).
  • I have absolutely no idea how to access the second element in the box (the first I can use the iterator since I don't increment it.
  • This is the outPut error from gdb:

    0 0x00002aaaaab43c65 in std::_Rb_tree_increment(std::_Rb_tree_node_base*) () from /usr/lib64/libstdc++.so.6
    1 0x000000000041a28e in operator++ (this=0x63d7c0) at /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_tree.h:265

I'll appreciate any help, thanks.

Edit:
Solved, kinda.

Trying to reduce the error to a minimum running example, I have deleted all those line and rewritten them and now things look fine. I'm still interested of such a case though:

If you want, I can reask the question:
When will iterator incremental cause a segmentation fault in c++?

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

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

发布评论

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

评论(3

你与昨日 2024-12-12 20:14:19

要回答您的编辑,唯一可能的方法是:

如果你愿意,我可以重新问这个问题:迭代器增量何时会导致 C++ 中的分段错误?

仅当使用不正确时,递增迭代器才会导致分段错误。

就是这样。

该标准没有列出代码必须出现段错误的情况,它只列出了正确使用代码时应该发生的情况。。当您错误地使用它时,这只是未定义的行为,并且可能会发生一百万种情况中的任何一种,包括但不限于分段错误。

如果增加迭代器会导致段错误,则可能是因为:

  • 迭代器已经指向集合的末尾,或者
  • 迭代器指向集合外部,指向超出范围的变量,或者迭代器可能从未指向集合的末尾。完全初始化。

但是当您有未定义的行为时,就会发生分段错误。根据定义,这使得无法告诉您哪些情况会触发分段错误。

To answer your edit the only way possible:

If you want, I can reask the question: When will iterator incremental cause a segmentation fault in c++?

Incrementing an iterator will cause a segmentation fault only if it is used incorrectly.

That's it.

The standard doesn't list the cases in which code must segfault, it only lists what should happen when code is used correctly. When you use it incorrectly, it is simply undefined behavior, and any of a million things might happen, including, but not limited to, segmentation faults.

If incrementing your iterator gave you a segfault, it is either because:

  • the iterator already pointed to the end of the set, or
  • the iterator pointed outside the set, to a variable that had gone out of scope, or perhaps the iterator had never been initialized at all.

But segmentation faults happen when you have undefined behavior. And that, by definition, makes it impossible to tell you which cases will trigger a segmentation fault.

瘫痪情歌 2024-12-12 20:14:19

这些行中至少存在一个错误:

  bit2 = beadsBoxes[t2].begin();
  while (bit2 != nextBoxes[t2].end()){
    shared_ptr<bead> b2 = *bit2++//the segmentation fault is here

假设 beadsBoxesnextBoxes 是标准容器类型的对象,则您错误地使用了迭代器。

bit2 似乎是一个指向 beadsBoxes 成员的迭代器。但是,您将其与 nextBoxes 的末尾进行比较。如果 beadsBoxes[2]nextBoxes[2] 代表不同的对象,则不能这样做。

At least one error is present in these lines:

  bit2 = beadsBoxes[t2].begin();
  while (bit2 != nextBoxes[t2].end()){
    shared_ptr<bead> b2 = *bit2++//the segmentation fault is here

Assuming beadsBoxes and nextBoxes are objects of standard container types, you are using the iterator incorrectly.

bit2 appears to be an iterator pointing at the members of beadsBoxes. But, you are comparing it to the end of nextBoxes. If beadsBoxes[2] and nextBoxes[2] represent different objects, you can't do that.

朦胧时间 2024-12-12 20:14:19

您应该修复的第一件事是 getBox。它不返回值,因此调用它时会发生未定义的行为。在末尾添加 return out;

The first thing you should fix is getBox. It does not return a value, so undefined behavior happens when it is called. Add return out; at the end.

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