1

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.

Sam Miller
24.2k4 gold badges70 silver badges91 bronze badges
asked Nov 23, 2010 at 0:00
7
  • Why do you have copy constructor and destructor call other functions instead of doing the work there? Commented Nov 23, 2010 at 0:09
  • 1
    In copy, you are not setting cursorM if (source.headM != NULL) (just something which struck me as odd) Commented Nov 23, 2010 at 0:11
  • Also, it is perfectly fine to assign ListItem's to eachother, you do not need to copy each member individually Commented Nov 23, 2010 at 0:12
  • @Falmarri You end up needing to do a copy and a destroy in multiple places in the code. It's pretty common to split them out. Commented Nov 23, 2010 at 0:13
  • @BrentNash: I was talking about copying the data value (struct ListItem), not the linked list nodes (struct Node). ListItem contains only primitive types, in my opinion using implicit copy constructors on structs with strictly only primitive types is clearer. Commented Nov 23, 2010 at 0:17

1 Answer 1

4

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!

answered Nov 23, 2010 at 0:08
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot for the help, it was the destroy() function causing the seg. fault. By not initializing cursorM within the copy function didn't affect anything as it is just used as a manipulator and is repeatedly reset to NULL but I did include it within my copy constructor as good practice. Thanks again

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.