|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#1 |
|
Member (10 bit)
|
odd segmentation fault...
I have 5 remove code segments to remove books from a linked list. each book has a category, so the main list plus the 4 category lists. i can remove the book from the main list, but using the same code modified for one of the sublists causes a segmentation fault???
code for main list remove: Code:
void library:: remove(char * target) {
Book * tptr;
Book * tptrF;
Book * tptrS;
Book * tptrC;
Book * tptrO;
tptr = ptFirstAll;
tptrF = ptFirstFiction;
tptrS = ptFirstScience;
tptrC = ptFirstChild;
tptrO = ptFirstOther;
char type;
while(tptr->title!=NULL) {
// check if book is last
if(tptr->ptAll == 0 && strcmp(tptr->title, target) == 0) {
tptr->ptAll = NULL;
countAll--;
type = tptr->type;
break;
}
// check if book is first
else if(strcmp(ptFirstAll->title, target) == 0) {
ptFirstAll = ptFirstAll->ptAll;
countAll--;
type = tptr->type;
break;
}
// check if book is in middle
else if(strcmp((tptr->ptAll)->title, target) == 0) {
tptr->ptAll = (tptr->ptAll)->ptAll;
countAll--;
type = tptr->type;
break;
}
tptr = tptr->ptAll;
}
Code:
if(type=='C') {
while(tptrC->title!=NULL) {
// check if book is last
if(tptrC->ptSub == 0 && strcmp(tptrC->title, target) == 0) {
tptrC->ptSub = NULL;
countC--;
return;
}
// check if book is first
else if(strcmp(ptFirstChild->title, target) == 0) {
ptFirstChild = ptFirstChild->ptSub;
countC--;
return;
}
// check if book is in middle
else if(strcmp((tptrC->ptSub)->title, target) == 0) {
tptrC->ptSub = (tptrC->ptSub)->ptSub;
countC--;
return;
}
tptrC = tptrC->ptSub;
}
}
anyone see what the problem is? thanks ^dan |
|
|
|
|
|
#2 |
|
Professional gadfly
|
I can't see anything from the code that is obvious. Maybe you could attach the entire code as a text file so I could see where the problem is.
What are you using for an IDE? Are you just using a compiler like g++? |
|
|
|
|
|
#3 |
|
Member (10 bit)
|
yea im on a unix server(g++). here is the code:
|
|
|
|
|
|
#4 |
|
Member (10 bit)
|
it is getting caught up in the remove last part... but it is not any different in that segment than any other segment???
^dan Last edited by GSXdan; 10-19-2003 at 08:00 PM. |
|
|
|
|
|
#5 |
|
Member (10 bit)
|
aha...
if i change the while header from while(tptr!->title) to while(tptr->ptSub), it gets rid of that seg fault(as long as i dont try to remove the windinthewillows. |
|
|
|
|
|
#6 |
|
Professional gadfly
|
Yes, I just discovered that book is the problem. It appears that your program is trying to remove that book from the list of Other books. Seeing as how it is a children's book, that isn't working. It appears that in the removal code for all books, it never gets to the section that checks if the book to be removed is last. I think that you either have to rework the section that applies to books in the middle to cover books at the end, or reorder things. The way it stands, it removes Wind in the Willows and sets its type to "O", presumably from Tora Tora Tora.
Can you do these assignments using anything else? It would really help to be able to debug your programs line by line in these situations. |
|
|
|
|
|
#7 |
|
Member (10 bit)
|
unfortunetly, we have to use linked lists. how else could i reorder it? for removing something, i dont think it matters what order it goes in, does it?
|
|
|
|
|
|
#8 |
|
Member (10 bit)
|
i dont get why it would be doing that. the remove statement before that removing "TheAtom" is also a last book, but it gets removed fine. Could it be a problem in the C add function maybe?
|
|
|
|
|
|
#9 |
|
Member (10 bit)
|
I think this is the problem. if you output the type before it goes to the remove sublists part, the types come out as CSCS, it should be OSCS(not including the windinthewillows). i just realized that einstiensimplified isnt being removed from the others sublist, but it is from the main, so that could be the source of all the problems....??
|
|
|
|
|
|
#10 |
|
Member (10 bit)
|
AHAHAHA that fixed it!!!! i just had to change how the type was assigned when it was the last book being removed. I had it jsut as type = tptr->type, but since i was using a peek-ahead method, i had to use type = (tptr->ptAll)->type
|
|
|
|
|
|
#11 |
|
Member (10 bit)
|
crap found another problem
when looking at my first output, two of the sublists are not sorted correctly. Fiction books: ---------- HuckleberryFinn 38476 GoneWithTheWind 82813 TheClient 84721 TomSawyer 42631 Science books: --------- AbriefHistoryofTime 12345 HowToProgram 99972 Physics 77362 Science 66333 TheAtom 87351 Children's books: --------- BigRed 98723 AlicInWonderland 11192 BigTree 23234 Garfield 93841 HandHandFingersThumb 10923 OldYeller 24351 WindInTheWillows 83741 Other books: -------- AsimovsMysteries 66621 EinsteinSimplified 88362 ToraToraTora 73731 in fiction books, huckleberryfinn should go after gonewiththewind. and in childrens books, bigred should go after alicinwonderland. that is also wierd as it is only those 2 functions??? oh well, back to work... thanks for all your help!!! |
|
|
|
|
|
#12 |
|
Member (10 bit)
|
Code:
}else if(strcmp(temp->title, ptFirstFiction->title) < 0) {
temp->ptSub = ptFirstFiction -> ptSub;
ptFirstFiction->ptSub = temp;
countF++;
return;
ptFirstFiction = temp, i completely lose the temp book. that seems like the logical way to do it.... check if temp book is less than first book if it is, set next field of temp to next field of current first set temp equal to first my logic off somewhere? |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|