Segmentation fault in linked list copy constructor

Hi, homework problem here. This should be simple to fix but I can't seem to figure it out. The problem lies in the copy constructor, I'll post the code I wrote for it and the functions called from it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//SkipList.h
#ifndef _SkipList_H
#define _SkipList_H

template <typename T>
struct Node
{
	T data;
	Node<T> *next;
	Node<T> *prev;
};

template <typename T>
class SkipList
{
public:
	SkipList();
	SkipList(const SkipList<T> & rhs);
	~SkipList();

	void printList();

	unsigned int size();
	bool empty();
	void clear();
	T front();
	T back();
	void pop_front();
	void pop_back();
	void push_front(const T& item);
	void push_back(const T& item);
	void insert_after(unsigned int position, const T& item);
	void remove(unsigned int position);

private:
	unsigned int this_size;

	Node<T> * begin;
	Node<T> * end;
	Node<T> * current;

	void init();
	void clone(const SkipList<T> & orig);
	Node<T> * find(unsigned int position);
};

#include "SkipList.inl"
#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//relevant functions from SkipList.inl

//copy constructor
template <typename T>
SkipList<T>::SkipList(const SkipList<T> & orig)
{
	init();
	clone(orig);
}

//clone segment reused in copy constructor and operator=
template <typename T>
void SkipList<T>::clone(const SkipList<T> & orig)
{
	Node<T> * temp = new Node<T>;
	temp = orig.begin;

	current = begin;
	
	while( current != 0 )
	{
		current->next = temp->next;
		current->prev = temp->prev;
		current->data = temp->data;
		temp = temp->next;
		current = current->next;
	}

	this_size = orig.this_size; //this causes the segfault (?)
}

//set up a new list
template <typename T>
void SkipList<T>::init()
{
	this_size = 0;
	
	begin = new Node<T>;
	end = new Node<T>;

	begin->next = end;
	begin->prev = 0;

	end->next = 0;
	end->prev = begin;
}


I've figured out that copying the 'this_size' variable (just an integer holding the size of the list) causes the segfault. I don't really understand why, and if I comment out that line it works fine and I can perform operations on the copied list just fine. I wouldn't think that copying over an integer, which uses no memory allocation, would cause a segfault...
Topic archived. No new replies allowed.