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 8850469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:54:39+00:00 2026-06-14T12:54:39+00:00

There are two sets of code that I think are equivalent, but one causes

  • 0

There are two sets of code that I think are equivalent, but one causes a seg fault and the other doesn’t. I’m really confused about why this is the case…

What I would like is to create a find function that

This code does work:

    MyPair <Comparable, BinomialNode<Comparable>*> dummy(x, NULL);
    MyPair <Comparable, BinomialNode<Comparable>*>* pair = hashTable.find(dummy);
    if(pair!=NULL)
    {
        addDupe(x, pair);   
    }

void addDupe( Word & x, MyPair<Word, BinomialNode<Word>*>* pair)
{
    list<int>::iterator it;
    list<int> lines = x.getLineNums();
    for ( it=lines.begin(); it != lines.end(); it++ )
    {
        pair->second->element.addLineNum(*it);
    }
}

The code below does NOT work. The changes from the above is that I tried to do was to try to move the find function out and make it return a BinomialNode* which would be the second element in the MyPair. addDupe in this version handles BinomialNode* instead of MyPair*.

I tracked the segfault to return pair->second.
Why does that cause a segfault but pair->second->element.addLineNum(*it) from above does not?

BinomialNode<Comparable>* node = find(x);
if(node!=NULL)
{
    addDupe(x, node);
}

BinomialNode<Comparable>* find(Comparable& x)
{
    MyPair <Comparable, BinomialNode<Comparable>*> dummy(x, NULL);
    MyPair <Comparable, BinomialNode<Comparable>*>* pair = hashTable.find(dummy);
    if(pair!=NULL)
         return NULL;
     return pair->second; //LINE CAUSES SEGFAULT
}

void addDupe( Word & x, BinomialNode<Word>* node)
{
    list<int>::iterator it;
    list<int> lines = x.getLineNums();
    for ( it=lines.begin(); it != lines.end(); it++ )
    {
            node->element.addLineNum(*it);
    }
}
  • 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-14T12:54:41+00:00Added an answer on June 14, 2026 at 12:54 pm
    if(pair!=NULL) //when pair is not 0
             return NULL;
    

    so after that you trying to access to NULL pointer what is cause of SEGFAULT

    You have to check for NULL:

    if(pair==NULL) //when pair is 0
                 return NULL;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I noticed that there are two sets of Hadoop configuration parameters: one with mapred.*
There are two sets of javascript SDK code, the one given on the SDK
I have read a document that they say: In java there two types of
There are two table s : one is the master and one the detail
There are two similar questions asked here and here but no adequate answers are
I'm using the trapz function on two sets of data and there's something wrong.
I'm working on rewriting some aging web applications. There are two in particular that
There are two intents on the receiver side which are called from the same
There are two lists: List<string> excluded = new List<string>() { .pdf, .jpg }; List<string>
There are two project in which I collaborate, they live in different servers, A

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.