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

try this

//function to clear all pointers
void clearMemory(bus* &pointer)
{
  bus *next;
  bandMember *current;
  
    
  next = pointer;
  while(next != NULL)
  {
      current = next->memListPtr;
      while(current)
      {
        bandMember *hold = current;
        current = current->memberPtr;
        delete hold;
      }
    bus* hold = next;
    next = next->busPtr;
    delete hold;

  } 
  pointer = NULL;
}

void Add(bus*& head)
{
    bus* node = new bus;
    node->busPtr = 0;
    node->memListPtr = 0;
    if(head == NULL)
        head = node;
    else
    {
        bus* next = head;
        while(next->busPtr)
            next = next->busPtr;
        next->busPtr = node;
    }
}

int main()
{
    bus* head = 0;
    for(int i = 0; i < 5; i++)
        Add(head);
    clearMemory(head);
}

that did it, thanks

struct node
{
    int data;
    struct node *link;
};


void delet(struct node **currNode,int location)
{
    int i;
    struct node *temp,*temp1,*temp2;
    int cnt;
    temp= *currNode;
    cnt=count(currNode);
    if(location>cnt)
    {
        printf("\nIndex should be <= %d",cnt);
    }
    for(i=1;i<=cnt;i++)
    {
        if(location==1&&i==1)
        {
            *currNode=temp->link;
            free(temp);
        }
        if(i==location-1)
        {
         temp1=temp->link;
         temp->link=temp->link->link;
         free(temp1);
        }
        temp=temp->link;
    }
}
int count(struct node **currNode)
{
    int count;
    struct node *temp;
    temp= *currNode;
    if(*currNode==NULL)
    {
        count=0;
    }
    else
        count=1;
    while(temp->link!=NULL)
    {
        temp=temp->link;
        count++;
    }
    return count;
}


void main()
{
    int value,index;
    struct node *currNode;
    currNode=NULL;
    printf("Enter Index : ");
    scanf("%d",&value);
//delete node in the given index of a link list
    delet(&currNode,value);
}
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.