C ++双重释放或损坏(输出)错误 [英] c++ double free or corruption (out) error

查看:73
本文介绍了C ++双重释放或损坏(输出)错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

打印输出后,出现双重释放或损坏(输出)"错误.但是,此错误仅在输入较小时出现.对于较大的输入,程序不会抛出该错误.当我在主体内部创建多维数组并将其删除时,我没有收到错误.我只在此处发布了与该问题相关的部分代码.请请说明如何解决该问题.

I am getting error of "Double free or corruption(out)" after I print my output. But this error is only coming for small inputs. For bigger inputs program doesn't throw that error. When I create the multidimensional arrays inside the main and delete them, I do not get the error. I have only posted the part of the code which is relevant to this issue here. Please kindly explain how to resolve the issue.

#include<iostream>
#include<vector>
using namespace std;

class Knapsack{
  public:
    int noItems, capacity, value, weight;
    int *weightArray, *valueArray;
    int **ValueMatrix, **BacktrackMatrix;
    vector<int> itemsChosen;
    ~Knapsack();
    void getInputs();              // reads in data
    void findItems();         // calculates best value of items
    void backTrack(int row, int col); // backtracks items selected
    void print();                       //prints out data
};

Knapsack::~Knapsack()
{
  delete[] weightArray;
  delete[] valueArray;
  for(int i=1;i<=noItems;i++)
  {
    delete[] ValueMatrix[i];
  }
  delete[] ValueMatrix;
  for(int i=1;i<=noItems;i++)
  {
    delete[] BacktrackMatrix[i];
  }
  delete[] BacktrackMatrix;
}

void Knapsack::getInputs()
{
  cin>>noItems;
  cin>>capacity;
  weightArray=new int[noItems];
  valueArray=new int[value];
  for(int i=1;i<=noItems;i++)
  {
    cin>>value;
    valueArray[i]=value;
  }
  for(int i=1;i<=noItems;i++)
  {
    cin>>weight;
    weightArray[i]=weight;
  }
  ValueMatrix=new int*[noItems];
  for(int i=1;i<=noItems;i++)
  {
    ValueMatrix[i]=new int[capacity+1];
  }
  BacktrackMatrix=new int*[noItems];
  for(int i=1;i<=noItems;i++)
  {
    BacktrackMatrix[i]=new int[capacity+1];
  }
}

int main()
{
  Knapsack *knap=new Knapsack();
  knap->getInputs();
  knap->findItems();
  knap->print();
  delete knap;
  return 0;
}

推荐答案

我认为,问题的根源是由于 valueArray 的分配以及您越界进行迭代的事实所致.

I believe the root of your issue is caused by the allocation of valueArray, and the fact that you are iterating out of bounds.

valueArray = new int [value]; 用大小为 value 的数组初始化 valueArray ,该数组是未初始化的变量.也许您打算使用 noItems ?

The line valueArray=new int[value]; initializes valueArray with an array of size value which is an uninitialized variable. Perhaps you meant to use noItems?

而且,正如松原瑶在评论中指出的那样,您的 for 循环看起来像 for(int i = 1; i< = noItems; i ++) 1 处,并以在 noItems 处的计数器结束,这是错误的.在包括C ++在内的许多语言中,数组都从索引 0 开始(这意味着第一项是 array [0] ,而不是 array [1] ),最后一项是减去数组大小的一个(因此,具有5个元素的数组的最后一项是 array [4] ).

Also, as songyuanyao pointed out in the comments, your for loops look like for(int i=1;i<=noItems;i++) which starts the counter at 1 and finishes with the counter at noItems, which is erroneous. In a lot of languages, C++ included, arrays start at index 0 (meaning the first item is array[0], not array[1]) and the last item is one minus the size of the array (so the last item of an array with 5 elements is array[4]).

如果将 for 循环更改为从0开始并在 noItems 之前结束一个元素,那么您应该会很高兴.那应该是 for(int i = 0; i< noItems; i ++)

If you change your for loop to start at 0 and end one element before noItems you should be golden. That would be for(int i = 0; i < noItems; i++ )

较小的分配可能发生的情况是,不同的内存块按顺序排列在内存堆的同一区域中,因此,当数据溢出缓冲区时,您正在砸碎 new 簿记数据.

What's probably happening with smaller allocations is the different chunks of memory are arranged sequentially in the same area of the memory heap, so when you overrun the buffer with data, you're smashing new's bookkeeping data.

当您有更大的分配时,新的内存就无法完全容纳到堆的可用空间中,因此分配器最终在分配之间留下了一些松弛空间.因此,小的溢出不会破坏堆信息.

When you have larger allocations, the new memory can't fit as cleanly into the free space of the heap, so the allocator ends up leaving some slack space between the allocations. Thus, a small overrun doesn't destroy heap information.

这篇关于C ++双重释放或损坏(输出)错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆