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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:17:18+00:00 2026-06-08T17:17:18+00:00

I’m writing some C++ code for a simple Node class. This is basically a

  • 0

I’m writing some C++ code for a simple “Node” class. This is basically a class used to manage a linear linked list. I normally perform this with a struct but I’m trying get a better handle of OOP and classes. What I’ve got thus far for the Node class is (note: the String class is my version (trimmed down) of a typical “string” class, it implements a copy constructor, assignment overload, destructor, etc. In testing it has worked great and seems completely self contained):

class Node {
public:

    //Constructor
    //-----------

    Node() : next_(0) {} //inline (String constructor called)

    //Destructor
    //----------

    ~Node(); 

    //Copy Constructor
    //----------------

    Node(const Node &);    

    //Operator Overload: =
    //---------------------
    //In conjunction with copy constructor.  Protects Class.

    Node & operator=(const Node &);   

private:

    String relatedEntry_;
    Node * next_;
};

Creating one instance works fine (ie. Node node;) but when I create an instance that calls the Copy Constructor I end up with segfaults at the very end of my program, as it’s cleaning up. The difference between using a struct for a linked list vs a class plays tricks with me a little and I think I’m missing something key here. Here is the implementation for the Default Constructor, Copy Constructor, and Overloaded Assignment Operator:

//Constructor inlined

//Destructor
Node::~Node()
{
    Node * curr = next_;
    while (curr) //cycle through LL and delete nodes
    {
        Node * temp = curr; //hold onto current
        curr = curr->next_; //increment one
        delete temp; //delete former current
    }
}


//Copy Constructor
Node::Node(const Node & cp)
{
    std::cout << "in CopyCon" << std::endl;
    relatedEntry_ = cp.relatedEntry_; //calls String class copy constructor/assignment overload
    Node * curr = cp.next_; //for clarity, for traversal
    while (curr) //copies related entry structure
    {
        Node * oldNext = next_;
        next_ = new Node;
        next_->next_ = oldNext; //'next' field (assign prior)
        next_->relatedEntry_ = curr->relatedEntry_; //String class copy
        curr = curr->next_; //increment
    }
}


//OO:  =
Node & Node::operator=(const Node & cp)
{
    std::cout << "in OO: =" << std::endl;
    if (this == &cp)
        return *this; //self assignment
    delete next_; //delete LL
    relatedEntry_ = cp.relatedEntry_; //String Class Assignment Overload
    Node * curr = cp.next_; //for clarity, for traversal
    while (curr)
    {
        Node * oldNext = next_; //hold onto old
        next_ = new Node; 
        next_->next_ = oldNext; //set next to old
        next_->relatedEntry_ = curr->relatedEntry_; //set this string to cp string
        curr = curr->next_; //increment
    }
    return *this;
}

Note that using the Overloaded Assignment Function seems to work fine (no segfaults) even though it’s virtually the same code… I’m assuming it has to do with the fact that both objects are already initialized before the assignment takes place?

//This seems to work ok
Node node1;
Node node2;
node2 = node1;

I’ve been at this bug for a couple of hours and I have got to get some rest. I’d really appreciate any insight into this. Thanks.

  • 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-08T17:17:19+00:00Added an answer on June 8, 2026 at 5:17 pm

    In the copy constructor loop, you have this line:

    Node * oldNext = next_;
    

    However, in the first round in the loop the value of next_ can by, well, anything and most likely not NULL. This means that the last node will a have a non-null pointer.

    Initialize it to NULL before the loop and it should work.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has

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.