【问题标题】:Why am I getting a "free(): invalid pointer Aborted (core dumped)" error? C++为什么我收到“free(): invalid pointer Aborted (core dumped)”错误? C++
【发布时间】:2020-07-23 01:20:29
【问题描述】:

我正在尝试重载 = 运算符以复制并最终交换两个双端队列?

但是我得到了 free(): invalid pointer Aborted (core dumped)

while 循环退出时出错。当 tempDeque.display(); 函数调用被注释掉时,一切运行正常。

这里是运算符重载的代码块:

class Resizeable_deque{
    private:
        T* array;
        int front;
        int back;
        int deque_size;
        int capacity;
        int initial_capacity;

    public:
        // Resizeable_deque();

        //accessors
        Resizeable_deque(int n=10) //done constructor
        {
            if (n==10)
            {
                array = new T[10];
                capacity = 10;
                initial_capacity = 10;
                front = -1;
                back = 0;
                deque_size = 0;
                // cout <<  "Capacity = " << capacity << endl;
                // cout << "OK!";

            }

            else if  (n<=10)
            {
                array = new T [n];
                capacity = n;
                initial_capacity = n;
                front = -1;
                back = 0;
                deque_size = 0;
                // cout <<  "Capacity = " << capacity << endl;

                // cout << "OK!!";
            }
        }

        ~Resizeable_deque(); //done destructor

        //accessors
        T* getArray() const;
        T getFront() const; // done get front element
        T getBack() const; //done get last element
        int size() const; //done get the size of the queue
        int getCapacity() const; //done get the current capacity of queue
        int getInitialCapacity() const;

        //data member setters
        // void setArray(T* newArray);
        // void setfront(int newFront);
        // void setBack (int newBack);
        // void setSize (int newSize);
        // void setCapacity (int newCapacity);
        // void setInitialCapacity(int newInitialCapacity);


        //state check functions
        bool full()  const;
        bool empty() const; //done check if empty
        T display() const;

        //mutators
        void push_front (const T e);
        void push_back (const T e);
        void pop_front();
        void pop_back();
        void pop_front_back();
        void resizeDeque(float resizefactor, int newsize);
        void swap(Resizeable_deque tempDeque);
        Resizeable_deque operator=(Resizeable_deque b)
        {
            cout <<"B:";
            b.display();
            cout << "Back: " << b.getBack() << endl;
            int bSize = b.size();
            Resizeable_deque<string> tempDeque(b.getCapacity());
            while (!b.empty())
            {
                tempDeque.push_back(b.getFront());
                // b.display();
                b.pop_front();
                cout<< "popped!" << endl;
                tempDeque.display();
                cout <<  "back is: " << b.getFront() << endl;
                cout << endl;
                cout << endl;
                cout << endl;
            }

            //end of loop call swap
        };
        void clear();

};

【问题讨论】:

  • 问题可能出在算子的原型上。你能把它和你的类定义一起粘贴吗?
  • 我怀疑您在未显示的代码中的某处有未定义的行为。请编辑您的问题以提供minimal reproducible example

标签: c++ class pointers overloading free


【解决方案1】:

当您通过值传递(例如,交换函数)时,您的复制构造函数将被调用。由于您尚未定义复制构造函数,因此将为您创建默认构造函数。该复制构造函数将仅复制 array 的指针值,这意味着您将有 2 个实例指向同一动态分配的内存。当这两个析构函数都运行时,它会释放内存两次,这会导致未定义的行为,这会显示为您看到的错误。

你基本上是在打破0/3/5的规则

一个简单的解决方法是创建数组std::unique_ptr&lt;T[]&gt;,这将导致构建错误,直到您解决问题为止。

【讨论】:

    【解决方案2】:

    我无法添加评论,所以我在这里写。

    我注意到您使用operater new[] 分配内存,您是否在使用new[] 分配的指针上使用了free()?这可能会导致错误。

    如果没有,请检查您是否在非原始指针上使用了free()(您是否在其上使用 ++ 或 -- 并更改其偏移量?)。

    否则,请检查您是否进行了一些越界访问。

    愿这些对你有所帮助。

    【讨论】:

    • 我无法运行你的代码,所以只能给点建议。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-18
    • 2021-10-03
    • 1970-01-01
    相关资源
    最近更新 更多