2016-11-15 62 views
0

所以这里是我的一个简单动态数组的工作代码。这必须是一个非常入门级的数据结构实现一个示例代码:Debug Assertion在动态数组上失败

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

所以每次我尝试运行这段时间我有2个问题:

enter image description here

enter image description here

我的代码有什么问题?为什么堆被损坏(我搜索了错误代码,这就是所有人都说的)?我的代码是否执行一些主要的访问违规?我正在使用VS2015。

+0

想想当你两次调用clear时会发生什么。 –

+1

...或清除并调用析构函数。 – Mat

+0

@RetiredNinja删除了一个清除,仍然错误仍然存​​在! –

回答

5

你做delete [] elements 倍之间没有设置elementsnullptr。这导致第二次(和第三次)未定义行为

+0

删除一个清除,仍然错误仍然存​​在! –

+0

@ Jeet.Deir这是因为你做*三次*。刚刚更新了我的问题,不要删除'clear'调用,将'elements'设置为'nullptr',你不必担心,允许在空指针上执行'delete []'' –

+0

更简单从清除中删除delete []''''''''''''''''''''''''''''''''''''''''''只能在构造函数中进行分配如果用户清除数组并试图添加一个值,它也会出现段错误 – Ari0nhh

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

您需要更改++sizesize++

+0

我仍然得到堆腐败检测:( –

+0

我看到清除看到叫过两次 – P0W

+0

删除一个清除,仍然错误仍然存​​在! –

相关问题