Hi! I found a bug in Vector class:
Line 177, Vector.h
/** \brief Adds several elements to the end of the Vector.
* \param ptr A pointer to the elements.
* \param num The number of elements.
*/
void add(const Type* ptr, int num) {
int neededCapacity = mSize + num;
if(mCapacity < neededCapacity) {
do {
mCapacity *= 2; // PAY ATTANTION HERE !!!!
} while(mCapacity < neededCapacity);
reserve(mCapacity); // AND NOW WE HAVE A BUG - SEE BELOW
}
for(int i=0; i<num; i++) {
mData[mSize++] = *(ptr++);
}
}
After the class member "mCapacity" has been changed the method "reserve" is executed:
Line 289, Vector.h:
/** \brief Reserves space in the Vector.
* \param newCapacity The desired capacity of the Vector.
* \note If \a newCapacity is less than the current capacity of the Vector, nothing will happen.
*/
void reserve(int newCapacity) { // ATTANTION! newCapacity is equal to mCapacity (SEE ABOVE)
MAUTIL_VECTOR_LOG("reserve 0x%08X %i", (int)this, newCapacity);
if(newCapacity <= mCapacity) // TRUE
return; // RETURN
MAUTIL_VECTOR_LOG("reserve 2");
Type* newData = new Type[newCapacity];
MAUTIL_VECTOR_LOG("reserve 4");
for(int i=0; i < mSize; i++) {
newData[i] = mData[i];
}
MAUTIL_VECTOR_LOG("reserve 5");
mCapacity = newCapacity;
delete[] mData;
MAUTIL_VECTOR_LOG("reserve 6");
mData = newData;
MAUTIL_VECTOR_LOG("reserve done");
}
As You can see "reserve" couldn't work properly when is is called from "void add(const Type* ptr, int num)" method.
But an other method "add" works properly because it doesn't attempt to change mCapacity member.
Line 164, Vector.h:
/** \brief Adds an element to the end of the Vector.
* \param val The element to be added.
*/
void add(const Type& val) {
if(mSize >= mCapacity-1) {
if(mCapacity != 0)
reserve(mCapacity*2);
else
reserve(4);
}
mData[mSize++] = val;
}
Best regards,
Alexander Galilov