C++ 调试断言在动态数组上失败

C++ 调试断言在动态数组上失败,c++,C++,下面是我为一个简单的动态数组编写的工作代码。这必须是非常入门级数据结构实现的示例代码: #include <iostream> using namespace std; class AdvancedArray { public: AdvancedArray(); ~AdvancedArray(); int get_size() const; // get the number of elements stored double& at(int

下面是我为一个简单的动态数组编写的工作代码。这必须是非常入门级数据结构实现的示例代码:

#include <iostream>
using namespace std;

class AdvancedArray {
public:
    AdvancedArray();
    ~AdvancedArray();
    int get_size() const; // get the number of elements stored
    double& at(int idx) const; // access the element at idx
    void push_back(double d); // adds a new element
    void remove(int idx); // remove the element at idx 
    void clear(); // delete all the data stored
    void print() const;

private:
    double* elements;
    int size;
};

int main()
{
    AdvancedArray* arr = new AdvancedArray();
    cout << "The Array Size is: " << arr->get_size() << endl;
    cout << "Pusing Values: 1.2, 2.1, 3.3, 4.5 in the Array. " << endl;
    arr->push_back(1.2);
    arr->push_back(2.1);
    arr->push_back(3.3);
    arr->push_back(4.5);
    arr->print();
    cout << "The Array Size is: " << arr->get_size() << endl;
    cout << "The Element at Index 2 is: " << arr->at(2) << endl;
    cout << "Deleting Values: 2.1 from the Array. " << endl;
    arr->remove(1);
    cout << "The Array Size is: " << arr->get_size() << endl;
    arr->print();
    cout << "Clearing the Array: " << endl;
    arr->clear();
    cout << "The Array Size is: " << arr->get_size() << endl;
    arr->clear();
    return 0;
}

AdvancedArray::AdvancedArray()
{
    size = -1;
    elements = new double[100]; //Maximum Size of the Array
}

AdvancedArray::~AdvancedArray()
{
    delete[] elements;
}

int AdvancedArray::get_size() const
{   
    if(size < 0)
    {
        return 0;
    }
    return size;
}

double & AdvancedArray::at(int idx) const
{
    if (idx < 100 && idx >= 0 && size > 0) {
        return elements[idx];
    }   
    cout << "Index Out of Bounds." << endl; 
}

void AdvancedArray::push_back(double d)
{
    if (size >= 100)
    {
        cout << "Overflow Condition. No More Space!" << endl;
    }
    else
    {
        elements[++size] = d;
        cout << "Element Pushed In Stack Successfully!" << endl;
    }
}

void AdvancedArray::remove(int idx)
{
    if (size >= 100 || size < 0)
    {
        cout << "No Such Element Exists!" << endl;
    }
    else
    {
        for(int i = idx; i <size; i++)
        {
            elements[idx] = elements[idx + 1];
        }
        size--;
        cout << "Element Deleted In Stack Successfully!" << endl;
    }
}

void AdvancedArray::clear()
{
    delete[] elements;
    size = -1;
}

void AdvancedArray::print() const
{
    cout << "[ ";
    for(int i = 0; i <= size; i++)
    {
        cout << elements[i] << "  ";
    }
    cout << "]" << endl;
}
#包括
使用名称空间std;
高级雪松{
公众:
advancedaray();
~z~雷();
int get_size()const;//获取存储的元素数
double&at(int-idx)const;//在idx处访问元素
void push_back(双d);//添加新元素
void remove(int idx);//删除idx处的元素
void clear();//删除存储的所有数据
无效打印()常量;
私人:
双*元素;
整数大小;
};
int main()
{
Advancedaray*arr=新的Advancedaray();
不能推回(4.5);
arr->print();

cout当
size==99
时,以下代码段将尝试访问
元素[100]

if (size >= 100)
{
    cout << "Overflow Condition. No More Space!" << endl;
}
else
{
    elements[++size] = d;
    cout << "Element Pushed In Stack Successfully!" << endl;
}
如果(尺寸>=100)
{

不能在不将
元素
设置为
nullptr
的情况下删除[]个元素
三次。这会导致第二次(和第三次)出现未定义的行为。

仍然检测到堆损坏:(想想当你两次调用
clear
或清除并调用析构函数时会发生什么情况。@RetiredInja删除了一个清除,但错误仍然存在!如果你在清除时删除内存块,那么在清除之后用户应该如何使用动态数组类?你手里有一个很好的调试器,按F10和F11,并知道确切的原因。询问p有建议的人不会有多大帮助。@Jeet.Deir这是因为你做了三次。只是更新了我的问题。不要删除
清除
调用,将
元素设置为
nullptr
,你不必担心。可以对空指针执行
delete[]
。最好简单地删除
delete[]
从清除。您只在构造函数中分配。如果用户清除数组并尝试添加值,它也将出错。