Hi, I believe my constructors are not correct. I keep getting this runtime error: invalid allocation size 4294967295 bytes. Can anyone identify the problem?
Here's the code to help you see what I'm working with:
private:
T* data;
unsignedint capacity;
unsignedint lastIndex;
template<class T>
DynamicArray<T>::DynamicArray(void) { //default constructor: makes an initial data array of size 10
data = new T[10];
}
template<class T>
DynamicArray<T>::DynamicArray(unsignedint initial) { //constructor for a specific initial size
if(initial > 0)
{
data = new T[initial];
}
else
{
data = NULL;
}
capacity = initial;
}
Just a little addition: Why are you testing a size_t aka unsigned int against 0, the check is redundant because you can't possibly assign a negative digit to a size_t and get a negative digit in return. If you accidentally or intentionally give unsigned integer variable a negative value, your compiler would generate code for a signed int literal and then move that into your unsigned int literal space.
tl;dr Don't bother checking if a size_t is lesser than 0, because it always is greater
1 2
unsignedint x = -5;
int y = x; // y is not -5 but std::numeric_limits<int>::max() - 5.