Template method

I need help with my code please...
it compiles successfully but when I run it, it throws out a segmentation fault and can't see the mistake.
The purpose of the program is to sort a integer list using bubble sort and insertion sort

Here's my code
Main.cpp
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
#include "BubbleSorter.h"
#include "InsertionSorter.h"

#include <stdlib.h>
#include <time.h> 

int main()
{
	// Set seed for random number generation
	srand(time(NULL));

	const int SIZE = 10;
	int values1[SIZE], values2[SIZE];

	for(int i = 0; i < SIZE; ++i)
	{
		// Generate two numbers between 1 and 100
		values1[i] = rand() % 100 + 1;
		values2[i] = rand() % 100 + 1;
	}

	Sorter *sorter;

	// Bubble sort
	sorter = new BubbleSorter(values1, SIZE);
	sorter->sort();
	delete sorter;
	
	// Insertion sort
	sorter = new InsertionSorter(values2, SIZE);
	sorter->sort();
	delete sorter;
	
	return 0;
}

Sorter.h & Sorter.cpp:
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#ifndef SORTER_H
#define SORTER_H

#include <string>

using namespace std;

class Sorter
{
	protected:
		int mSize;
		int mStartIndex;
		int *mValues;
	
	public:
		Sorter(int *values, int size);
		~Sorter();
		void sort();
	
	private:
		void print();
		virtual void innerLoop (int index)=0;
		virtual string getName() =0;
};

#endif




#include <iostream>
#include <string>
#include <string.h>
#include "Sorter.h"

using namespace std;

Sorter::Sorter(int values[], int size)
{
	mSize = size;
	
	for(int g = 0; values[g] != '\0'; g++)
	{
		mValues[g] = values[g];
	}
	
}
Sorter::~Sorter()
{
	delete []mValues;
}
void Sorter::print()
{
	
	for(int i=0; i<mSize; i++)
	{
		cout << mValues[i] << " ";
	}
	cout << endl;
	
	cout << "PRINT" << endl;
}

void Sorter::sort()
{
	cout << getName() << endl;
	print();
	for(int x = mStartIndex; mStartIndex < mSize; mStartIndex++)
	{
		innerLoop(x);
		print();
	}
}




BubbleSortter.h & BubbleSorter.cpp
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
49
50
51
52
53
54
55
#ifndef BUBBLESORTER_H
#define BUBBLESORTER_H
#include "Sorter.h"
#include <string>

using namespace std;

class BubbleSorter: public Sorter
{
	private:
		void innerLoop(int index);
		string getName();
	
	public:
		BubbleSorter (int values[], int size);
};

#endif




#include <iostream>
#include "BubbleSorter.h"

using namespace std;

BubbleSorter::BubbleSorter (int values[], int size)
	:Sorter(values, size)
	{
		mStartIndex = 0;
	}
		
string BubbleSorter::getName()
{
	return "Bubble Sort:";
}

void BubbleSorter:: innerLoop(int index)
{
	for(int index=0; index<mSize; index++)
	{
		for(int k=0; k<mSize-1; k++)
		{
			if(mValues[k]>mValues[k+1])
			{
				int temp = mValues[k+1];
				mValues[k+1] = mValues[k];
				mValues[k] = temp;
			}
		}
	}
	
}




InsertionSorter.h & InsertionSorter.cpp
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
49
50
51
52
53
54
55
56
57
#ifndef INSERTIONSORTER_H
#define INSERTIONSORTER_H
#include "Sorter.h"
#include <string>

using namespace std;

class InsertionSorter: public Sorter
{
	private:
		void innerLoop(int index);
		string getName();
	
	public:
		InsertionSorter (int *values, int size);
};

#endif




#include <iostream>
#include <string>
#include "InsertionSorter.h"
#include "Sorter.h"
using namespace std;

InsertionSorter::InsertionSorter (int values[], int size)
	:Sorter(values, size)
	{
		mStartIndex = 1;
	}

void InsertionSorter::innerLoop(int index)
{
	index = mStartIndex;
	 int j ,tmp;
	for (index; index < mSize; index++) 
	{
		j = index;
	}
	if (j > 0 && mValues[j - 1] > mValues[j]) 
	{
		tmp = mValues[j];
		mValues[j] = mValues[j - 1];
		mValues[j - 1] = tmp;
		j--;
	}
	cout << index << endl;
}
string InsertionSorter::getName()
{
	string inSort;
	inSort ="Insertion Sort:";
	return inSort;
}




Thanks in advance guys, will appreciate any help a lot!
My first guess would be in your 'Sorter' constructor. There is no guarantee that an arrays contents are set to NULL when it is allocated so don't delimit the loop based on that. Also you don't allocate any memory to your 'mValues' data member before you just start writing to offsets from it.

You are already passing the size of the array in as an argument to this constructor so use the 'new' operator to allocate memory for 'mValues' based on that. Then use that same variable to limit your for loop when copying. You could alternatively just point 'mValues' at the address of the array that gets passed in but then if that array were to go out of scope then the behavior becomes undefined and you're in another kind of mess.
Last edited on
You need to declare the `Sorter' destructor as virtual, because you've got at least one virtual member function.

I could not reproduce your issue.
Run through a debugger and perform a backtrace when it crashes.
Topic archived. No new replies allowed.