I've been working on custom Vector class. Everything works perfectly fine on Microsoft compiler, however when I try it on Borland I'm getting a really strange error.
Borland throws the exception inside the insert function; Exactly when calling the copy constructor "Vector temp(*this);" at the
"array_ = new int[rhs.size_];" line.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
void Vector::insert(int value, unsigned position) throw(SubscriptError)
{
check_bounds(position);
Vector temp(*this);
int tmpSize= size_;
temp.size_++;
size_++;
for (unsignedint i=tmpSize; i > position; i--)
{
temp[i] = temp[i-1];
}
temp[position] = value;
//array_= temp.array_;
for(unsignedint i = 0; i < size_; i++)
{
array_[i]= temp.array_[i];
}
}
here is my copy constructor(where the error is thrown);
You're having an out-of-bounds access in insert.
In the first iteration of the moving loop, you're trying to assign the element at index size_+1 to size_ - that's two elements off the mark. You're never resizing the array anywhere.
And there's absolutely no reason to perform two full copies of the entire vector every time you insert an element. That'll result in terrible performance.