Front Office Football Central  

Go Back   Front Office Football Central > Archives > FOFC Archive
Register FAQ Members List Calendar Mark Forums Read Statistics

Reply
 
Thread Tools
Old 03-28-2005, 03:40 AM   #1
Karim
College Starter
 
Join Date: Oct 2000
Location: Calgary
c++: seg fault hunting

I'm working on a doubly linked list.

Synopsis:
- add item (ok)
- remove item by position in list (display shows it's gone)
- add item to same location //seg fault

OR

- add item (ok)
- remove item by position in list (display shows it's gone)
- add item to different position in list (ok)
- remove item by position in list //it still accesses the location of the first item - seg fault

I've traced a bunch but since it works in some situations, I can't see what's wrong.

Here are the problematic functions:
Code:
bool To_Do_List::remove(int position) { int priority; bool success = false; To_Do_List::Item_Ptr target; success = search(position, target, priority); if(success == true) { unlink(target); delete target; } return success; }

Code:
bool To_Do_List::search(int pos, Item_Ptr &item, int &priority)const { bool found = false; int index; int count = 0; To_Do_List::Item_Ptr curr; for(index = 2; index >= 0; index--) { curr = head[index].next; if(curr != NULL) { count++; } if(pos == count) { item = curr; priority = index; found = true; return found; } else if(curr == NULL) { } else if(curr != NULL && pos != count) { while(curr -> next -> next != NULL && count != pos) { curr = curr -> next; count++; } if(pos == count) { item = curr; priority = index; found = true; return found; } } } return found; }

Code:
void To_Do_List::unlink(Item_Ptr item) { To_Do_List::Item_Ptr curr; To_Do_List::Item_Ptr prev; curr = item -> next; prev = item -> prev; prev -> next = curr; curr -> prev = prev; item -> next = NULL; item -> prev = NULL; }

Code:
class To_Do_List { public: To_Do_List(); ~To_Do_List(); bool read(ifstream &input); bool write(ofstream &output)const; void display(int priority, int &position, ostream &out)const; void add(const char desc[], int priority); bool remove(int position); bool update(int position, int priority); bool lookup(int position, char desc[], int &priority)const; private: struct Item { Item *next; Item *prev; char desc[MAX_DESC_LEN + 1]; }; typedef Item *Item_Ptr; bool search(int pos, Item_Ptr &item, int &priority)const; Item_Ptr create_item(const char desc[])const; void link(Item_Ptr item, int priority); void unlink(Item_Ptr item); void display_priority(int priority, int &position, ostream &out)const; Item head[NUM_PRIORITIES]; Item tail[NUM_PRIORITIES]; };


Although the search function may not be sleek, I don't think the problem is there because another function not included uses it successfully. If I run the program without remove, I never get a seg fault.

Karim is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is On
Forum Jump


All times are GMT -5. The time now is 07:10 AM.



Powered by vBulletin Version 3.6.0
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.