Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there a Trace/Breakpoint error using Delete[] in c++?

I am currently working on making a "vector" class.

template <typename T>
class Vectors{
private:
        int size_;
public:
        int size_;
        T *elements;
        Vector(){
            size_=0;
            T *elements=new T[size_];
        }
        Vector(int size){
            size_=size;
            T *elements=new T[size_];
        }
        void push_back(T amaze){
            //make new temporary pointer;
            T *Temp=new T[size_+1];
            size_=size_+1;
            for(int i=0;i<size_-1;i++)
                {
                    *(Temp+i)=*(elements+i);
                }
            delete[] elements; //Error occurs here
            elements=NULL;
            elements=Temp;
            *(elements+size_-1)=amaze;
        }
}

After running the debugger, I found out that there is a trace/breakpoints trap when the program reaches delete[] elements.

Why is this error occurring?

like image 283
Last Aeon Avatar asked Jan 18 '26 02:01

Last Aeon


2 Answers

Enable warnings, and you will see that you are not initializing the elements member but a variable:

T *elements=new T[size_];

So when you delete them, you are basically trying to delete a broken pointer that has never been allocated and points nowhere. This makes the program crash, effectively.

By the way, you should post the actual code you are running, because currently your class name is Vectors, plural; but the constructors are called Vector. A proper constructor would use initializer lists and would be explicit:

explicit Vector(std::size_t size)
: size_(size), elements_(new T[size_])
{
}

The default constructor should not try to allocate an array of size 0. Just keep elements as nullptr.

like image 131
Acorn Avatar answered Jan 20 '26 18:01

Acorn


Your constructors are not assigning anything to the class elements member. They are assigning to local variables of the same name, shadowing the class member. So the class member is still uninitialized when push_back() tries to delete[] it.

Change

T *elements=new T[size_];

To

elements=new T[size_];

Also note that your class does not follow the Rule of 3/5/0, as it lacks a destructor, copy constructor, and copy assignment operator. And it is declaring the size_ member twice, which should fail to compile.

Try this:

template <typename T>
class Vector{
private:
    int size_;
    int count_;
    T *elements;
public:
    Vector(){
        size_ = 0;
        count_ = 0;
        elements = new T[size_];
    }

    Vector(int size){
        size_ = size;
        count_ = 0;
        elements = new T[size_];
    }

    Vector(const Vector &v){
        size_ = v.size_;
        count = v.count_;
        elements = new T[size_];
        for(int i = 0; i < count_; ++i) {
            elements[i] = v.elements[i];
        }
    }

    ~Vector() {
        delete[] elements;
    }

    void push_back(T amaze){
        if (count_ == size_) {
            T *Temp = new T[size_ * 2];
            for(int i = 0; i < count_; ++i) {
                Temp[i] = elements[i];
            }
            delete[] elements;
            elements = Temp;
            size_ *= 2;
        }
        elements[count_]= amaze;
        ++count_;
    }

    void swap(Vector &other) {
        std::swap(elements, other.elements);
        std::swap(size_, other.size_);
        std::swap(count_, other.count_);
    }

    Vector& operator=(const Vector &v) {
        if (&v != this) {
            Vector(v).swap(*this);          
        }
        return *this;
    }
};
like image 45
Remy Lebeau Avatar answered Jan 20 '26 16:01

Remy Lebeau



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!