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

  • Home
  • SEARCH
  • 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 8330427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:07:13+00:00 2026-06-09T02:07:13+00:00

I have a single depth-first search algorithm implemented to traverse my graph, given the

  • 0

I have a single depth-first search algorithm implemented to traverse my graph, given the iterator to a starting node.

Documentation summary:

  • GraphIter is a typedef for Graph::iterator
  • Graph class extends map<string, Node>
  • start->second.edges() returns set<string>

This code causes a segmentation fault if the size of start->second.edges() is 0:

(I’ve truncated the irrelevant pieces, including the recursive calls, for brevity.)


Bad Code

void Graph::dfs(GraphIter start)
{
    cout << "EDGES SIZE: " << start->second.edges().size() << endl;

    for (set<string>::iterator it = start->second.edges().begin();
          it != start->second.edges().end(); ++it)
    {
        GraphIter iter = this->find(*it);     // <--- SEGMENTATION FAULT
    }
}

Now watch what happens when I pull start->second.edges() into a local variable: no more segfault!

Here’s the code that doesn’t generate a segfault:


Good Code

void Graph::dfs(GraphIter start)
{
    set<string> edges = start->second.edges();       // <--- MAGIC TRICK
    cout << "EDGES SIZE: " << edges.size() << endl;

    for (set<string>::iterator it = edges.begin();
          it != edges.end(); ++it)
    {
        GraphIter iter = this->find(*it);
    }
}

So the difference is that in the good code, when the size of the set of strings (from the edges() method) is 0, the for loop is never entered in the second case. But in the first case, the for loop is still executed at least once until it realize that it can’t dereference the it variable.

Why are these different? Don’t they access the same parts of memory?

  • 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-06-09T02:07:14+00:00Added an answer on June 9, 2026 at 2:07 am

    Because edges() returns a set by value, start->second.edges().begin() and start->second.edges().end() return iterators to different containers because each call to edges() results in a new set being returned.

    By creating a single copy with a named variable you ensure that the iterators all come from the same container and you can validly iterator from begin() to end().

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

Sidebar

Related Questions

i have a single filed which i need to clone with a clone button
I have A single page that has the following structure <form runat=server><placeholder></placeholder></form> I have
i have a single video (duration: 3 seconds) and i need to create 2
I have a single-view application that takes in a username and a password and
I have a single page website that changes content based on variables passed through
I have a single-threaded application that uses 3 SQLite databases in 3 different files
I have a single view application. It has myAppDelegate and myViewController. In a earlier
I have one single file called Exercises.rb def ask(prompt) print prompt, ' ' $stdout.flush
I have a single big text file in which I want to process each
I have a single data context (created once), and I use it to get

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.