I'm having Buss Error issues. I feel that it has to do with either my deconstuctor or my insert/delete functions.

I know I'm not supposed to put up whole source code, but I will put it up if needed.

Assume that ItemType is an integer and Next is a pointer.

void  ItemList::Insert(ItemType  item)
{
      NodePtr currPtr;
    NodePtr prevPtr;
    NodePtr location;
    location = new  NodeType;
	 location->item = item;
	  prevPtr = NULL;
	 currPtr = listPtr -> next;
	 while (currPtr != NULL  &&  item != currPtr->item ) 
	 {	
        prevPtr = currPtr; 	   // Advance both pointers 
        currPtr = currPtr->next;
	  }
	  location->next = currPtr;// Insert new node here
	  if  (prevPtr == NULL) 
        listPtr -> next = location;
	  else
		 prevPtr->next = location;
  // FILL IN Code.
}

//***********************************************************

void  ItemList::Delete(ItemType  item)
{
  NodePtr   delPtr;
    NodePtr   currPtr; // Is item in first node? 		    
    if (item == listPtr->item)
    {	// If so, delete first node	
        delPtr = listPtr;		        
        listPtr = listPtr->next;
    } 				
    else // Search for item in rest of list
    {
        currPtr = listPtr;
        while (currPtr->next->item  !=  item)
       currPtr = currPtr->next;
       delPtr = currPtr->next;
       currPtr->next = currPtr->next->next;
	  }
    delete  delPtr;
}

ItemList::~ItemList()
// Post: All the components are deleted.
{
	while (listPtr -> next != NULL)
	delete listPtr;
	if(listPtr -> next == NULL)
	delete listPtr;
}

>while (listPtr -> next != NULL) delete listPtr;
Unless listPtr->next is NULL on the first iteration, you're deleting the same pointer multiple times. Try something more like this for the whole destructor:

while (listPtr != NULL) {
  NodePtr save = listPtr->next;
  delete listPtr;
  listPtr = save;
}

>while (listPtr -> next != NULL) delete listPtr;
Unless listPtr->next is NULL on the first iteration, you're deleting the same pointer multiple times. Try something more like this for the whole destructor:

while (listPtr != NULL) {
  NodePtr save = listPtr->next;
  delete listPtr;
  listPtr = save;
}

Okay that makes sense. I thought you could go through and destroy the whole list at once. Now the bus error is still there, not specifically telling me what is causing the error. I'm now assuming that it's not the deconstructors but the constructors .

// Post: listPtr is set to NULL.
{
         listPtr = NULL;
  // FILL IN Code.
}

//***********************************************************

ItemList::ItemList(const ItemList& otherList)
{
   listPtr = otherList.listPtr;
  // FILL IN Code.

}

I think it's the handling of my default constructor.

Edited 6 Years Ago by Kirielson: n/a

This question has already been answered. Start a new discussion instead.