c++ - 试图删除指针触发断点

标签 c++ class pointers delete-operator

我正在处理的程序的目的是创建一个类,通过模拟动态指针数组来“改进”默认整数数组数据类型。在尝试删除指针和指针数组时,我一直遇到错误,其中显示“Windows 已触发 project4.exe 中的断点。

这可能是由于堆损坏,这表明 project4.exe 或其加载的任何 DLL 中存在错误。

这也可能是由于用户在 project4.exe 具有焦点时按了 F12。

输出窗口可能有更多诊断信息。"

class Array
{
private:
    int length;
int* data;
public:
    Array();
    Array(const Array &cpy);
    ~Array();
    bool addint(int toadd);
    bool deletelast();
    int getlength();
    friend ostream& operator<<(ostream &out, const Array &n);
};

ostream& operator<<(ostream &out, const Array &n);

Array::Array()
{
    length = -1;    
    data = NULL;
}

Array::Array(const Array &cpy)
{  
    length = cpy.length;                //value of length is copied

    if (length < 0)
        data = NULL;
    else
    {
        data = new int [length];

        for (int i=0; i<=length; i++)
            data[i] = cpy.data[i];
    }

}    

Array::~Array()
{
    if (length != 0)
        delete [] data;
    else
        delete data;

    data = NULL;
}  

bool Array::addint(int toadd)
{   
    length ++;
    int* point = new int[length];

    for (int i=0; i < length; i++)
        point[i] = data[i];             

    point[length] = toadd;

    if (length != 0)    
        delete [] data; 

    data = point;

    point = NULL;

    return true;
}    

bool Array::deletelast()
{
    int* temppoint;
    if (length > 0)
        temppoint = new int [length-1]; 
    else
        temppoint = new int[0];

    for (int i=0; i<length; i++)        
        temppoint[i] = data[i];

    if (length == 0)
        temppoint[0] = 0;

    length --;
    delete [] data; 
    data = temppoint;
    temppoint = NULL;

    return true;
}  

void menu(Array var)
{
    int selection=0,
        input;
    bool success;
    Array* arrcpy;
    while (selection != 3)
    {
        if (var.getlength() == -1)
        {
            cout << "What would you like to demonstrate?" << endl << "1) Add an integer " << endl
            << "2) Exit" << endl << "Enter your selection: ";
            cin >> selection;
            if (selection == 2)
                selection = 4;
        }
        else
        {
            cout << endl << "Now what would you like to demonstrate?" << endl << "1) Add an integer " << endl
            << "2) Delete the last entered integer" << endl << "3) Copy constructor" << endl << "4) Exit" << endl << "Enter your selection: ";
            cin >> selection;
        }

        if (selection==1)
        {
            cout << endl << "The length of the array before adding a new value is: " << var.getlength() + 1 << endl;
            cout << "Please enter the integer that you wish to add: ";
            cin >> input; 
            success = var.addint(input);
            if (success)
                cout << endl << "The data input was a success!" << endl << "The length of the array is now: " 
                << var.getlength() + 1 << endl << "The new value of the array is: " << var << endl;
            else
                cout << endl << "The input failed" << endl;
        }       
        if (selection == 2)
        {
            cout << endl << "The lenght of the array before the deletion is: " << var.getlength() + 1 << endl 
                << "and the value held in the array is: " << var << endl;
            success = var.deletelast();
            if (success)
                cout << endl << "The data deletion was a success!" << endl << "The length of the array is now: "
                << var.getlength() + 1 << endl << "The new value of the array is: " << var << endl;
            else
                cout << endl << "The deletion failed" << endl;
        }       
        if (selection == 3)
        {
                cout << endl << "The lenght of the array being copied is: " << var.getlength() + 1 << endl 
                << "and the value held in the array is: " << var << endl;
                arrcpy=new Array(var);
                cout << endl << "The length of the copied array is: " << arrcpy->getlength() +1 << endl
                << "and the value contained in the array is: " << *arrcpy;
                delete arrcpy;
        }
    }
}

这是我所拥有的与我遇到的问题相关的所有源代码。 delete 运算符和 delete [] 运算符的每个实例都会导致此断点错误,我不确定我做错了什么。

编辑:重写代码,使长度值默认为 0 而不是 -1,现在一切正常!

最佳答案

我相信for (int i=0; i<=length; i++)在复制构造函数中应该包含 i<length (小于,不小于或等于)。这是一个明显的问题。

此外,您还通过了 addint() 中的界限方法。数组中的最后一个元素位于索引 [length-1]。

关于c++ - 试图删除指针触发断点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13898707/

相关文章:

c++ - 如何仅在输入变量后而不是立即开始使用 getline?

c++ - 为 std::vector<double> boost 自定义验证器

c++ - visual studio 2013 中的 C++ 链接错误

java - class.class 代表什么

c++ - 如何确定对象指针指向的类型?

c - 如何修复与 C 中的动态数组指针/堆栈相关的崩溃?

c++ - 在父类中包含子类的 typedef

javascript - 'this'在javascript中的对象内部未定义,通过另一个属性访问对象的属性

Java MultiChoice,2 个错误答案?

c++ - 有没有办法在递归中使用指针来查找数组的最小值?