954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

doubly linked list seg fault help

I have a simple doubly linked list for a class I am in and I keep getting a seg fault on my last line of code, no matter what i make it. I can put a bunch of random couts and it will print them all then on the last one seg fault, all my test functions are working how they should too. Any idea what would cause a seg fault? only things in the class are what my prof says are the typical functions... The big 3, popBack/front, pushback/front and a few others that already work as they should... Only thing i can think of is the destructor causing a seg fault. the destructor just calls my popback function which the code for is below...could this be the problem? any help is appreciated.

void Dll11List::popBack(){
	Dll11Node* nodeptr;
	if(isEmpty()){
		cerr<<"\nList is Empty";
		return;
	}
	if(_first->_fore == NULL){
		delete _last;
		_first = NULL;
		_last = NULL;
	}else{
		nodeptr = _last->_back;
		delete _last;
		_last = nodeptr;
	}
	--_size;
}
wnr78ta
Newbie Poster
15 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 
DllllNode* temporaryTail = _last;

    _last = _last->_back;

    delete temporaryTail;
LevyDee
Posting Whiz in Training
260 posts since Mar 2010
Reputation Points: 13
Solved Threads: 25
 

I tried these changes and its still giving me a seg fault on the last line of code. maybe thats not the problem. What else could cause a seg fault on the last line of code no matter what that line of code is?
Thanks

wnr78ta
Newbie Poster
15 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 
What else could cause a seg fault on the last line of code no matter what that line of code is?


Corrupted memory that only manifests in the destructor is a good start.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Think of the case of a list with 2 elements. Once you've deleted the last one, where does _first->_fore point to?
BTW, same applies to _last->_fore in a general case.

nezachem
Posting Shark
903 posts since Dec 2009
Reputation Points: 719
Solved Threads: 194
 

well thats what i was thinking, but the destructor calls a function called delAll0() which simply calls the popback function which the code for is above, its supposed to delete the last node. i have a loop as well that says while(!isEmpty) then just called the popBack function. can you see anything wrong with the popBack function?

wnr78ta
Newbie Poster
15 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

I am just really confused on this because the way my prof does things ive never done them this way before...I am assuming my function is mostly right since that is what my prof gave us in class for the popBack function but I just dont see how to fix it. any help is great.
Thanks

wnr78ta
Newbie Poster
15 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: