Hi I am doing an assignment and my professor gave us the code for a copy function for a doubly linked list. The function should copy list into another empty list and if the list being copied is empty it should just return and exit. Here is the code my prof gave me that is giving me a seg fault and I have no idea what is wrong. Any help is appreciated.

void Dll11List::copy(const Dll11List & orig){
		cerr<<"\nCalling DLL is not empty";
	Dll11Node *iterorig = orig._first;
		_first = new Dll11Node(iterorig->_data);
		Dll11Node *iterhere = _first;
	while(iterorig->_fore != NULL){
		iterorig = iterorig->_fore;
		iterhere->_fore->_back = iterhere;
		iterhere = iterhere->_fore;
		_last = iterhere;
		_size = orig.getSize();


5 Years
Discussion Span
Last Post by wnr78ta

a seg. fault... couldn't you give us the complete source code so we can test our work? how could i do this if i don't test it???


i mean the complete code is 6 files...wasnt sure if i should post 6 files...I just know that this is the function calling it, it was the only function i was testing at the time.


line#14: You have never done a "new Dll11Node()" for "iterhere->_fore" and trying to access "iterhere->_fore->_back".

This is an obvious segfault.

Use gdb to debug your code if you are working on Unix/Linux. Its not that tough to catch a segfault.


I dont really understand what your saying, i delcared iterhere = _first, _first is the node and each node contains the pointers _fore and _back so if iterhere is _first then it should already have access to the _fore and _back so why would i need to declare the new node?

Also I would use gdb but our prof doesnt let us, actually ive tried on these programs but the way he sets it up so we have only 1 line of code in the main i have not been able to figure out how to step through the program built like this it will just run the entire program and say a segfault was returned.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.