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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:57:16+00:00 2026-05-18T06:57:16+00:00

I’m writing a program as an assignment for school and I though I had

  • 0

I’m writing a program as an assignment for school and I though I had worked out all the bugs until I decided to call my copy constructor. The program is interactive (CLI) which basically has two moduals: a .h and .cpp file for the LList class and a .h and .cpp file for the structure of the program and also a 3rd cpp file just for main(). It is suppose to be a program for a hydropower engineering company (fake company) in which the LList nodes hold data for their annual water flow in a river(year and flow). Here is some insight on the class:

//list.h
struct ListItem {
  int year;
  double flow;
};

struct Node {
  ListItem item;
  Node *next;
};

class FlowList {
public:
  FlowList();
  // PROMISES: Creates empty list

  FlowList(const FlowList& source);
    // REQUIRES: source refers to an existing List object
    // PROMISES: create the copy of source

  ~FlowList();
    // PROMISES: Destroys an existing list.

  FlowList& operator =(const FlowList& rhs);
  // REQUIRES: rhs refers to an existing FlowList object
  // PROMISES: the left hand side object becomes the copy of rhs

  //....Other member functions

private:
  // always points to the first node in the list.

  Node *headM;
  // Initially is set to NULL, but it may point to any node.

  Node *cursorM;
  //For node manipulation within interactive CLI

  void copy(const FlowList& source);

  void destroy();

I belive the memory leak or collision is taking place somewhere within the copy function but cant pin point where.

//list.cpp

FlowList::FlowList() : headM(0), cursorM(0) {}

FlowList::FlowList(const FlowList& source) 
{
  copy(source);
}

FlowList::~FlowList() 
{
  destroy();
}

FlowList& FlowList::operator =(const FlowList& rhs) 
{
  if (this != &rhs)
  {
    destroy();
    copy(rhs);
  }
  return (*this);
}

//...more function definitions

void FlowList::copy(const FlowList& source)
{
  if (source.headM == NULL) 
  {
    headM = NULL;
    cursorM = NULL; 
    return;
  }

  Node* new_node = new Node;
  assert(new_node);
  new_node->item.year = source.headM->item.year;
  new_node->item.flow = source.headM->item.flow;
  new_node->next = NULL;    
  headM = new_node;

  Node* thisptr = new_node;

  for(Node* srcptr = source.headM->next; srcptr != NULL; srcptr = srcptr->next)
  {
    new_node = new Node;
    assert(new_node);
    new_node->item.year = srcptr->item.year;
    new_node->item.flow = srcptr->item.flow;
    new_node->next = NULL;
    thisptr->next = new_node;
    thisptr = thisptr->next;
  }   

}

void FlowList::destroy()
{
  Node* ptr = headM;
  Node* post = headM->next;

  while (ptr != NULL)
  {
    delete ptr;
    ptr = post;
    if (post)
       post = ptr->next;
  }
  headM = NULL;
}

The program works fine if I create a FlowList object, fill it with data from a .dat file; i can then manipulate the data within the program (display, perform calculations, add to the list, remove from list and save data to file) but program crashes (segmentation fault) if I create another FlowList object (within main.cpp).
Any help would be really 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-05-18T06:57:16+00:00Added an answer on May 18, 2026 at 6:57 am

    The initial thing I spot is that it looks like your destroy() function will always segmentation fault if the list is empty:

    void FlowList::destroy()
    {
      Node* ptr = headM;
      Node* post = headM->next;
      //...
    }
    

    If the list is empty, headM is NULL and then you’re trying to do headM->next which will always produce a segmentation fault in that case.

    I think you might also have a memory leak in your copy constructor if you pass in an empty list. If you look at this code:

    void FlowList::copy(const FlowList& source)
    {
      if (source.headM == NULL) 
      {
        headM = NULL;
        cursorM = NULL; 
        return;
      }
      //...
    }
    

    What if the current list contained 20 items and source is an empty list? You set the current list’s headM and cursorM to NULL, but you never call delete on any of the nodes in the current list that you originally used new to create. You probably want to work your destroy() function somewhere into your copy constructor too (you did it for the operator= function).

    The last thing I noticed is that you don’t initialize cursorM for a non-empty list in your copy() function (@Akusete pointed that out as well). I think I’d recommend that at the beginning of your copy constructor, just initialize cursorM and headM to NULL just to cover your bases.

    It looks like you’re really close, I think you just really need to think through the boundary case of dealing with empty lists (both on the LHS and RHS) and you’ll probably find most of these bugs. Good luck!

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

Sidebar

Related Questions

No related questions found

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.