Fix memory deallocation bug in toy vector class.

Yet another reason to use standard collection classes. :-)
This commit is contained in:
Jack Palevich 2009-11-27 11:51:36 +08:00
parent b18c1507f8
commit ba48fe2f4e
1 changed files with 12 additions and 4 deletions

View File

@ -82,9 +82,7 @@ template<class E> class Vector {
~Vector() {
if (mpBase) {
for(size_t i = 0; i < mUsed; i++) {
mpBase[mUsed].~E();
}
clear();
free(mpBase);
}
}
@ -110,10 +108,20 @@ template<class E> class Vector {
* ensure(1) = item;
}
size_t size() {
inline size_t size() {
return mUsed;
}
void clear() {
if (mpBase) {
size_t used = mUsed;
for(size_t i = 0; i < used; i++) {
mpBase[i].~E();
}
}
mUsed = 0;
}
private:
E* ensure(int n) {
size_t newUsed = mUsed + n;