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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:57:13+00:00 2026-06-13T22:57:13+00:00

I am new to gdb and debugging in general, granted I am not sure

  • 0

I am new to gdb and debugging in general, granted I am not sure how I have made it this far without gdb. The program debugging that gdb tells me is that there is a segmentation fault in this function. I will highlight it with a comment below:

template <class elemType>
struct nodeType
{
    elemType info;
    nodeType<elemType> *lLink;
    nodeType<elemType> *rLink;
};

#define H_binarySearchTree
#include <iostream>
#include "binaryTree.h"

using namespace std;

template <class elemType>
class bSearchTreeType: public binaryTreeType<elemType>
{
public:
    bool search(const elemType& searchItem) const;
    //Function to determine if searchItem is in the binary
    //search tree.
    //Postcondition: Returns true if searchItem is found in
    //               the binary search tree; otherwise,
    //               returns false.

    void insert(const elemType& insertItem);
    //Function to insert insertItem in the binary search tree.
    //Postcondition: If there is no node in the binary search
    //               tree that has the same info as
    //               insertItem, a node with the info
    //               insertItem is created and inserted in the
    //               binary search tree.

    void deleteNode(const elemType& deleteItem);
    //Function to delete deleteItem from the binary search tree
    //Postcondition: If a node with the same info as deleteItem
    //               is found, it is deleted from the binary
    //               search tree.
    //               If the binary tree is empty or deleteItem
    //               is not in the binary tree, an appropriate
    //               message is printed.
    void printTree();
    void printTree(nodeType<elemType> *p);
    // void printTreeNode(nodeType<elemType> *p);
    void swapSubtrees(nodeType<elemType> *root1);
    void swapSubtrees();
    nodeType<elemType> *root1;

    template <class elemType>
    void bSearchTreeType<elemType>::insert
    (const elemType& insertItem)
    {
        nodeType<elemType> *current; //pointer to traverse the tree
        nodeType<elemType> *trailCurrent; //pointer behind current
        nodeType<elemType> *newNode;  //pointer to create the node

        newNode = new nodeType<elemType>;
        newNode->info = insertItem;
        newNode->lLink = NULL;
        newNode->rLink = NULL;

        if (root1 == NULL)
            root1 = newNode;
        else
        {
            current = root1;
            while (current != NULL)
            {
                trailCurrent = current;

                if (current->info == insertItem)//** This is where gdb says there is a seg fault
                {
                    cout << "The item to be inserted is already ";
                    cout << "in the tree -- duplicates are not "
                    << "allowed." << endl;
                    return;
                }
                else if (current->info > insertItem)
                    current = current->lLink;
                else
                    current = current->rLink;
            }//end while

            if (trailCurrent->info > insertItem)
                trailCurrent->lLink = newNode;
            else
                trailCurrent->rLink = newNode;
        }
    }//end insert

    template<class elemType>
    void bSearchTreeType<elemType>::swapSubtrees(nodeType<elemType> * root1)
    {

        if (root1 != NULL)
        {
            nodeType<elemType> *temp;

            swapSubtrees(root1->lLink);//Seg Fault here as well
            swapSubtrees(root1->rLink);

            temp =root1->lLink;

            root1->lLink = root1->rLink;
            root1->rLink = temp;
        }
    }
}

Could someone please help describe what is going on, and what I need to do to fix it?

*edited to include more details
Much Appreciated

  • 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-13T22:57:13+00:00Added an answer on June 13, 2026 at 10:57 pm

    You forgot to set root1 to NULL in your constructor – in fact, you don’t seem to have one.

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

Sidebar

Related Questions

I am new to debugging an assembly code under gdb.Therefore, I have some question;
I was just debugging a program in gdb and somehow I found a new
New to PHP and MySQL, have heard amazing things about this website from Leo
I have an odd bug in my program, it appears to me that malloc()
I asked a question just yesterday which caused this new issue. gdb within emacs
I have code similar to the following that interacts with gdb from Java. I
GDB gives me the above error WRT my C++ program. Nowhere I have used
Okay, I'm NOT new to Objective-C but I encountered a strange bug, that i
I have searched for topics specifically to this question but have not found anything
I am debugging a program using gdb. Whenever I miss a breakpoint or decide

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.