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

Dani AI

Generated

As hinted, the array size is determined by what the SortData constructor does at runtime — the max=100 in the declaration is only a default used when a caller omits an argument. What matters is whether the constructor allocates on the heap (or uses a container) using the value passed in.

A few important points not yet covered in the thread:

  • The base class needs a virtual destructor when it declares virtual functions; otherwise deleting derived objects through a base pointer leads to undefined behavior.
  • If raw arrays are used, allocate and free consistently (new[] / delete[]) and make sure the element type matches (the header shows long *theData; so allocation must use long).
  • With raw pointers implement or disable copy constructor and copy assignment to avoid shallow copies and double deletes (see special-member semantics).

For cleaner, safer code prefer RAII containers or smart pointers. For example, use std::vector<long> for the storage: it automatically allocates to the requested size, provides size(), and handles copying/cleanup correctly. Alternatively, std::unique_ptr<long[]> gives automatic deletion for an array but still requires manual resizing/copy semantics if needed. See:

Also note a likely compile-time pitfall in the posted main: calling test(InsertionSort(100)) is binding a temporary to a non-const lvalue reference, which is not permitted in standard C++. Either pass a named object (create the sorter first and then call test) or change the API to accept a pointer or appropriate smart pointer. Finally, watch algorithmic complexity: Insertion Sort is O(n^2) and will be slow for very large datasets — use std::sort or a well-implemented quicksort for large N.

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.