whenever the clearMemory function is called i get a segmentation fault and i can not figure out whats wrong. The function is supposed to delete all pointers. The function brings in the pointer to the linked list by reference. The nodes in the list are structs i named bus that contain another liked list i called bandmembers.
//band member struct to hold band member info
struct bandMember
{
//first name holder
string firstName;
//last name holder
string lastName;
//instrument holder
string instrument;
//pointer to another bandmember
bandMember *memberPtr;
};
//struct to hold bus info
struct bus
{
//bus designation
int busNum;
//bus capasity, same for all buses
int busCap;
//list of bandmembers on each bus
bandMember *memListPtr;
//pointer to another bus struct
bus *busPtr;
};
the function prototype and definition are:
//function prototype
void clearMemory(bus* &ptr);
//function to clear all pointers
void clearMemory(bus* &pointer)
{
bus *last, *next;
bandMember *current, *previous;
last = pointer;
next = last;
current = last->memListPtr;
previous = current;
while(last != NULL)
{
while(current != NULL)
{
previous = current;
current = current->memberPtr;
delete previous;
}
next = last;
last = last->busPtr;
delete next;
current = last->memListPtr;
previous = current;
}
pointer = NULL;
}
thank you in advance for any help