Checking for valid array size?

Will this be enough to ensure a valid array?

if (max > 1)

I need to write a constructor for a class that creates an array of size max (or 100 if not specified). Obviously I cannot accept a value such as -1 but I am not sure if it can be broken in another way that the check for a positive value will not be enough. Here is the function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
QuickSort::QuickSort (int max = 100) 
{
	if (max > 1) //If the input is valid or not specified and deflats to 100...
	{
		theData = new long[max];
		maxSize = max;
	}
	else //If the input is invalid...
	{
		theData = new long[100];
		maxSize = 100;
	}

}


Thank you in advance for any thoughts.

EDIT: I forgot to mention that the variable "maxSize" is inherited from the protected section of a base class from which I am deriving the other classes (such as my QuickSort class). maxSize is of type int.
Last edited on
Asking the user to input array size is always dangerous!!!(what if he enters 20000 or some large no, which may not be available).
Have a fixed upper limit like 200 and check for it.
I agree that there are a lot of silly things about this (why use a long when an int is the same size in my IDE???) but it is for an assignment for school so I must do what I can without changing how the class works.

For this assignment I will be comparing sorting algorithm efficiencies for n up to over 1,000,000 in some cases so I probably shouldn't fix an upper limit on it for this particular assignment, though I agree that it is a good suggestion for writing actual code someone might use.

Thank you for the post.
If u are using large inputs of upto 1,000,000 or more, pls make sure u have exception handling for memory failures else ur program mite crash during runtime, n ull be left wondering the reason!!
Topic archived. No new replies allowed.