Li Zeng
Li Zeng

Reputation: 35

C++: Memory Leak;Vector-like class

Im beginnger in C++ and try to creat container class which is similar to the Vector. this class should works like a Vector for all type of data and could be used in range-based for loop. i wrote the hpp but my tutor says that there is a memory leak ,I think i deleted all the dynamic memory,where could be the problem?

#include "stdafx.h"
using namespace std;
template<class T>
class Customvector
{
public:
    Customvector();
    ~Customvector();
    int size();
    int free_capacity();
    void add(T& temp);
    int& operator[](int index);
    void grow();

    class iterator {
        public:
            iterator(T* ptr) : ptr(ptr) {}
            iterator operator++() { iterator i(ptr); ++ptr; return i; }
            bool operator!=(const iterator & other) { return ptr != other.ptr; }
            const T& operator*() const { return *ptr; } 
        private:
            T* ptr;
        };

    iterator begin() const { return iterator(&_elements[0]); }
    iterator end() const { return iterator(&_elements[0]+_size); }
private:
    T*  _elements;
    int _size;
    int _capacity;
    int DEFAULT_CAPACITY;
};

template<class T>
Customvector<T>::Customvector()
{
    DEFAULT_CAPACITY = 4;
    _capacity = DEFAULT_CAPACITY;
    _size = 0;
    _elements = new T[_capacity];

}
template<class T>
Customvector<T>::~Customvector()
{
    delete[] _elements;

}
template<class T>
void Customvector<T>::add(T& temp)
{
    grow(); //check if the capacity is full, if so,increase capacity by DEFAULt_CAPACITY;
    _elements[_size++]= temp;

}
template<class T>
int Customvector<T>::size()
{
    return _size;
}

template<class T>
int Customvector<T>::free_capacity()
{
    int free_c = _capacity - _size;
    return free_c;
}


template<class T>
int& Customvector<T>::operator[](int index) {
    if (index<0 || index>_capacity)
    {
        cout << "index beyond limit" << endl;
        return _elements[0];
    };
    return _elements[index];
}

template<class T    >
void Customvector<T>::grow() 
{ 
    if (_capacity == _size) 
    {
        _capacity += DEFAULT_CAPACITY;
        T* p = new T[_capacity];
        std::copy(_elements, _elements + _size,p);
        delete[] _elements;
        _elements = p;
    };

}

Upvotes: 2

Views: 136

Answers (1)

eerorika
eerorika

Reputation: 238351

The only leaky case that I can find is in grow:

    ...
    T* p = new T[_capacity];
    std::copy(_elements, _elements + _size,p); // may throw an exception
    delete[] _elements;
    _elements = p;
    ...

If copying of a contained element throws, then _elements still points to the old array and the new array pointed by p leaks. You can resolve this with unique_ptr:

std::unique_ptr<T[]> p(new T[_capacity]);
std::copy(_elements, _elements + _size, p.get()); // it's OK if this throws, unique_ptr will take care of the memory
delete[] _elements;
_elements = p.release();

Using unique_ptr for _elements too would simplify some of your code and improve correctness.

Upvotes: 3

Related Questions