Loans | Loans | Mortgage Calculator | Record Internet Radio with Tags | Credit Cards
odd segmentation fault... [Archive] - PCMech Forums

PDA

View Full Version : odd segmentation fault...


GSXdan
10-19-2003, 03:11 PM
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:


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;


}


and here is the code for the segment causing the problem:


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

doctorgonzo
10-19-2003, 07:33 PM
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++?

GSXdan
10-19-2003, 08:14 PM
yea im on a unix server(g++). here is the code:

GSXdan
10-19-2003, 08:46 PM
it is getting caught up in the remove last part... but it is not any different in that segment than any other segment???
^dan

GSXdan
10-19-2003, 09:04 PM
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.

doctorgonzo
10-19-2003, 09:04 PM
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.

GSXdan
10-19-2003, 09:08 PM
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?

GSXdan
10-19-2003, 09:13 PM
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?

GSXdan
10-19-2003, 09:42 PM
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....??

GSXdan
10-19-2003, 09:46 PM
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

GSXdan
10-19-2003, 09:49 PM
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!!!

GSXdan
10-19-2003, 10:01 PM
}else if(strcmp(temp->title, ptFirstFiction->title) < 0) {
temp->ptSub = ptFirstFiction -> ptSub;
ptFirstFiction->ptSub = temp;
countF++;
return;


that is the section of code for finding out if it goes first, but it doesnt work. if it try to put
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?