*EDIT* IT IS A SEGMENTATION FAULT NOT BUS ERROR (anyway to change thread title?)

I am trying to make a deep copy of a linked list, but whenever I run a test of the constructor i get a "Segmentation Fault". I tested the "Append" method and it works fine. I can't figure out why I am getting this error unless there is something wrong with "Append" though

Involved code:

Copy Contstructor:

List(const List &L){
       ListIterator p;         
       p.currentPtr=L.head;
       while(p.currentPtr->next!=L.head){
               p.currentPtr=p.currentPtr->next;
               this->Append(p.currentPtr->item); 
               listSize++;
               }          
               }  // copy constructor (*must* be a deep copy)

method Append:

void Append(int it){
            ListIterator p;
            p.currentPtr=this->head;
	     ListElement *temp= new ListElement();  
            temp->item=it;
		if(this->IsEmpty()){
                            this->head->next=temp;
				this->head->prev=temp;
				temp->next=this->head;
				temp->prev=this->head;   
                               }         
            else{ 
                    temp->next=p.currentPtr;
                    temp->prev=p.currentPtr->prev;
                    p.currentPtr->prev->next=temp;
                    p.currentPtr->prev=temp;
                    p.currentPtr=temp;
                    }
	 
                 listSize++;
}

Dani AI

Generated

— the segfault you see is almost always caused by dereferencing an uninitialised sentinel. Your Append assumes this->head is a valid sentinel node (so head->next/prev exist). In the copy constructor you never create that sentinel for the new list, so the first call into Append touches this->head when it is null and crashes. The gdb hint you posted (error inside the Append block) matches that.

Fix checklist (minimal, safe approach):

  1. In the copy constructor create a sentinel node for the new list and set its next/prev to point at itself. Set listSize = 0.
  2. Walk the source list from src.head->next until you loop back to src.head, and for each real node append its item to the newly-initialised list.
  3. Do not increment listSize in the copy constructor if Append already updates it — that causes double counting.
  4. Implement the destructor and copy-assignment operator too (Rule of Three) so copies and destructions are safe.

Suggested algorithm (pseudocode):

create sentinel for target: head->next = head->prev = head; listSize = 0
for node = src.head->next; node != src.head; node = node->next
    Append(node->item)   // Append should update listSize

Testing/troubleshooting: step through the copy constructor in gdb to confirm this->head is non-null before the first Append call. Use Valgrind to catch invalid reads/writes and double frees. Finally, consider writing a copy constructor that builds nodes directly (instead of calling Append) if you want slightly better performance, but always ensure sentinel and size are consistent. For background on managing copy/destroy/assign semantics, see the Rule of Three: Rule of Three.

Recommended Answers

All 3 Replies

Out of curiosity are you taking cmps 260? We covered this recently.
I wish I could be of some solution I am just curious because I am trying to learn the same thing

Out of curiosity are you taking cmps 260? We covered this recently.
I wish I could be of some solution I am just curious because I am trying to learn the same thing

I am taking a Systems Programming course in which we work with C++

*also when i run the program in gdb it says that that line 7 of the second code block causes the error

Hmm Ive never seen a double -> -> reference yet I am about on the same lesson you are. I couldn't tell you if it was right or not but id check it out

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.