【问题标题】:Trying to delete pointers triggers breakpoints试图删除指针会触发断点
【发布时间】:2012-12-16 04:49:10
【问题描述】:

我正在开发的程序的目的是创建一个类,通过模拟动态指针数组来“改进”默认整数数组数据类型。尝试删除指针和指针数组时,我一直遇到错误,其中显示“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,现在一切正常!

【问题讨论】:

    标签: c++ class pointers delete-operator


    【解决方案1】:

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

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

    【讨论】:

    • 长度的值默认为-1,并且每次将数字添加到“数组”时都会递增,因此如果我将其设为i
    • @user1907309 你在滥用术语长度。长度不能为负数,它应该是i&lt;length 你正在使用它的东西好像是previousIndex
    • 项目要求它被称为长度,就像我想的那样,它会从 -1 开始,并在每次调用函数时递增到实际值。唯一一次值为 -1 时,指针的值为空。也许是我在这里说话的经验不足,但我不太确定为什么会导致这个问题。
    • @user1907309 那么你可能想要清理混乱,如果只是为了让这些问题更容易被发现。这样想..如果你的数组有一个项目,它的长度是多少?
    【解决方案2】:
    int* point = new int[length];
    

    由于您的长度从 -1 开始,因此该行的第一个调用将是执行 new int[0]。可能是问题.. 如果你坚持不修复 length 的语义,你想要 length+1 这里

    无关的一点,你应该看看std::vector是怎么做的,而不是重新分配每个添加,你应该尝试过度分配,只有在空间被填满时才重新分配。

    【讨论】:

    • 非常感谢您的意见。我使用默认长度 0 重写了它,现在它工作得很好。我仍然不明白它为什么会这样做,但无论哪种方式,由于您的意见,我让它工作,如果我将来再次遇到这样的事情,我会注意到这一点。但再次感谢您的意见。
    • @rob630 它不起作用,因为您混淆了 length 的含义。如果数组中有 5 个元素,则长度应为 5,但在您的情况下为 4。它在总是分配 1 个短的 addint 函数中出错,因为它假定长度表示长度,在默认情况下-1,它没有
    • 哦...哇,当然。这很有意义。我是根据元素来考虑它的,数组 [5] 将是数组 [x],其中 x 将是数字 0-4……这是一个愚蠢的错误。再次感谢您的帮助,尤其是帮助我理解我的错误。 :D
    【解决方案3】:

    我知道这是一个旧线程,我承认我没有阅读所有评论,但是关于为什么当长度更改为 0 时它似乎工作的主题我相信这可能是由于给出了 nullptr到一个大小无效的数组,然后尝试删除这样的指针会导致断点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-02
      • 2020-10-04
      • 1970-01-01
      • 1970-01-01
      • 2013-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多