Why does set iterator pointer cause segmentation fault?

Yotam Source

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++?

c++iteratorsegmentation-faultset

Answers

answered 7 years ago Robᵩ #1

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.

answered 7 years ago jalf #2

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.

answered 7 years ago Derek Ledbetter #3

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.

comments powered by Disqus