I've been trying to code an array where its size is dynamically allocated. It's been a while since I've coded and so I was wondering if you guys could give me some help? There's also a randomize function that allocates the array with a random set of numbers (the quantity of these "numbers" that are allocated into the array is dynamically allocated depending on the parameter value that is passed). If this doesn't make sense, refer to the code below. I'll get straight to the point and just paste in the part that matters..

My main .cpp

#include <iostream>
#include <iomanip>

using namespace std;
#include "InsertionSort.h"
#include "QuickSort.h"

void test(SortData& ad, bool print=false)
{
	ad.randomize();
	if(print)
	{
		cout << "Unsorted:" << endl;
		ad.printSome();
	}

	_int64 num = ad.sort();
	if(print)
	{
		cout << "Sorted:" << endl;
		ad.printSome();
	}

	cout << "Dataset Size= " << setw(8) << ad.size();
	cout << " Num Ops = "<< num << endl;
}

int main(int argc, char *argv[])
{
	cout << "Insertion Sort testing: " << endl;
	InsertionSort is10(10);
	test(is10, true);

	test(InsertionSort(100));
	test(InsertionSort(1000));
	test(InsertionSort(10000));
	test(InsertionSort(100000), true);
	cout << endl;

	cout << "QuickSort testing:" << endl;
	test(QuickSort(10), true);
	test(QuickSort(100));
	test(QuickSort(1000));
	test(QuickSort(10000));
	test(QuickSort(100000), true);
	test(QuickSort(1000000));
	return(0);
}

The header for InsertionSort

#include "SortData.h"

class InsertionSort : public SortData
{
public:
	InsertionSort(int max=100);
	~InsertionSort(void);
	_int64 sort();

private:
	_int64 numops;
	void insertionsort(long theArray[], int n);
};

The header for SortData

#include <iostream>

using namespace std;

class SortData
{
public:
	SortData(int max=100);
	~SortData(void);

	int size() const;
	void randomize(int seed = 1);
	void printSome(const int num=10) const;

	virtual _int64 sort() = 0;

protected:
	long *theData;
	int maxSize;
};

As you can see, SortData.h and InsertionSort.h has a constructor that initializes the array "theArray" to whatever "max" is set to. Looking at the main.cpp you can notice that the function "test" is called with "InsertionSort(size)" where size is defined by the parameter that is passed (ie 100, 1000, 10000, etc).

So I guess what I'm confused about is the max=100 parameter. This "max" with a initialized value of 100 is passed to SortData(max). Would the code that I've written (theArray[max];) only initialize the array "theArray" with a size of 100? How can I make this array dynamic so that it can be allocated with whatever size the system calls it to be (ie 1000, 10000, etc)?

Thanks all.

J

The array in SortData is potentially a dynamic array. It depends on what you do in the constructor. To me it looks like the constructor will look something like:

SortData::SortData(int max) : maxSize(max){
   theData = new int[max];
}

There are a number of problems with this constructor related to exception safety but, if you ignore those, SortData contains a dynamically allocated array. The 100 in the declaration of the constructor is a default parameter, ie if you just do SortData myData() in your code, the array in myData will be given that size, otherwise it will be the size that you specify in the constructor.

If you do this, you should make sure that you make the destructor delete the memory for the array, otherwise you will leak memory :o)

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.