动态数组上的调试断言失败

时间:2016-11-15 13:52:03

标签: c++

所以这是我的简单动态数组的工作代码。这必须是非常入门级数据结构实现的示例代码:

#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;
}

因此,每次尝试运行此操作时,我都遇到了两个问题:

enter image description here

enter image description here

我的代码出了什么问题?为什么堆被破坏(我搜索了错误代码并且所有人都要说)?我的代码是否在执行一些重大访问冲突?我正在使用VS2015。

2 个答案:

答案 0 :(得分:5)

您执行delete [] elements 三次次而未将elements设置为nullptr。这导致未定义的行为第二次(和第三次)。

答案 1 :(得分:2)

size == 99时,以下代码会尝试访问elements[100]

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

您需要将++size更改为size++