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){
	if(!isEmpty()){
		cerr<<"\nCalling DLL is not empty";
		return;
	}
	if(orig.isEmpty()){
		return;
	}
	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();
}

thanks

Dani AI

Generated

Following and : the crash is the expected result of dereferencing an uninitialized pointer. In the posted loop the code advances iterorig and then uses iterhere->_fore->_back — but iterhere->_fore was never assigned, so it holds garbage (or NULL) and the attempt to access _back triggers the segfault. A pointer to a node does not implicitly create a node; the new node must be allocated and linked before any field of it is read or written.

A safe, minimal approach is: create the first node from orig._first, set its _back to NULL, then for each next source node allocate a new node, set its _back to the current tail, set its _fore to NULL, link the current tail _fore to it, and advance. Example:

void Dll11List::copy(const Dll11List &orig) {
    if (!isEmpty()) { cerr << "\nCalling DLL is not empty"; return; }
    if (orig.isEmpty()) return;

    Dll11Node *src = orig._first;
    _first = new Dll11Node(src->_data);
    _first->_back = NULL;
    Dll11Node *dst = _first;
    src = src->_fore;

    while (src != NULL) {
        Dll11Node *n = new Dll11Node(src->_data);
        n->_back = dst;
        n->_fore  = NULL;
        dst->_fore = n;
        dst = n;
        src = src->_fore;
    }

    _last = dst;
    _last->_fore = NULL;
    _size = /* copy size from orig, e.g. orig.getSize() */;
}

Notes and cautions: ensure _first->_back and _last->_fore are explicitly set to NULL so other code relying on those invariants won't misbehave. If copies into a non-empty list must be supported, clear the destination first (or use copy-and-swap) to avoid leaks. When gdb is unavailable, lightweight diagnostics (temporary cout of pointer values and step markers) or running under Valgrind will quickly show the invalid dereference; a small standalone test harness that builds short lists and calls copy is also helpful.

Recommended Answers

All 4 Replies

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.