Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6616371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:34:29+00:00 2026-05-25T20:34:29+00:00

Bottom line, why would iterator cause segmentation fault? Note, I’m typing here the relevant

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:34:29+00:00Added an answer on May 25, 2026 at 8:34 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to add a data table to bottom of a (line) chart
Can anyone conjure from this code why the ItemsSource line would be getting a
You can skip to the bottom line if you don't care about the background:
There is a way to keep the scroll on bottom for a multi line
Most popular browsers, while rendering an image, they display it line-by-line top-to-bottom as it
(resolved: see bottom) I have the following code snippet: Protected Sub SqlDataSource1_Inserted(ByVal sender As
I would like to send someone the Xcode simulator version - not the device
According to this article: http://subsonicproject.com/docs/3.0_Migrations Bottom line: if you're a developer that is concerned
Can someone summarise the key pieces of objective-c code that would assist in solving
Bottom up or top down? First I used top down but I felt to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.