View Single Post
Join Date: Sep 2008
Posts: 20
Reputation: kyosuke0 is an unknown quantity at this point 
Solved Threads: 0
kyosuke0 kyosuke0 is offline Offline
Newbie Poster

linked list deleteing problem

 
0
  #1
Nov 20th, 2008
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.

  1. //band member struct to hold band member info
  2. struct bandMember
  3. {
  4. //first name holder
  5. string firstName;
  6.  
  7. //last name holder
  8. string lastName;
  9.  
  10. //instrument holder
  11. string instrument;
  12.  
  13. //pointer to another bandmember
  14. bandMember *memberPtr;
  15. };
  16.  
  17. //struct to hold bus info
  18. struct bus
  19. {
  20. //bus designation
  21. int busNum;
  22.  
  23. //bus capasity, same for all buses
  24. int busCap;
  25.  
  26. //list of bandmembers on each bus
  27. bandMember *memListPtr;
  28.  
  29. //pointer to another bus struct
  30. bus *busPtr;
  31. };


the function prototype and definition are:



  1. //function prototype
  2. void clearMemory(bus* &ptr);
  3.  
  4.  
  5. //function to clear all pointers
  6. void clearMemory(bus* &pointer)
  7. {
  8. bus *last, *next;
  9. bandMember *current, *previous;
  10.  
  11.  
  12. last = pointer;
  13. next = last;
  14. current = last->memListPtr;
  15. previous = current;
  16.  
  17. while(last != NULL)
  18. {
  19. while(current != NULL)
  20. {
  21. previous = current;
  22. current = current->memberPtr;
  23. delete previous;
  24.  
  25. }
  26. next = last;
  27. last = last->busPtr;
  28. delete next;
  29. current = last->memListPtr;
  30. previous = current;
  31.  
  32. }
  33.  
  34. pointer = NULL;
  35. }


thank you in advance for any help
Last edited by kyosuke0; Nov 20th, 2008 at 6:53 pm.
Reply With Quote