为什么这段代码会导致"heap corruption detected"?
Why does this code lead to "heap corruption detected"?
我的代码有问题,我不明白它有什么问题。
这是代码中出现问题的原因:
void Safe_Array::resize(unsigned new_capacity)
{
score += sizeof(int)*(new_capacity - m_capacity);
if (m_capacity < new_capacity)
{
int old_len = m_capacity - 1;
Safe_Array temp(*this);
if (m_data)
delete[] m_data;
m_data = new int[sizeof(int)*new_capacity]; // new allocation
m_capacity = new_capacity;
for (int i = 0; i < old_len + 1; i++)
m_data[i] = temp.m_data[i];
for (; old_len < new_capacity; old_len++)
m_data[old_len] = 0;
}
else // here we need to shorten the array
{
Safe_Array temp(*this);
if (m_data)
delete[] m_data;
m_data = new int[sizeof(int)*new_capacity]; // new allocation
m_capacity = new_capacity;
for (int i = 0; i < new_capacity; i++)
m_data[i] = temp.m_data[i];
}
}
当我尝试delete m_data
时遇到错误。
此功能的用途:
首先,我创建了一个对象,m_data是它的数组,resize 是一个函数,正如它所说,它会"调整"对象的这个字段。
这是头文件:
class Safe_Array
{
public:
Safe_Array(unsigned capacity = 0, const int max_tries = 3);
Safe_Array(const Safe_Array&);
~Safe_Array();
void show(void) const;
unsigned get_capacity() const;
bool insert(int, unsigned);
bool get(unsigned index, int &value) const;
bool search(int value, unsigned &index) const;
Safe_Array& assign(const Safe_Array&);
void resize(unsigned);
void sort();
static unsigned get_score();
friend int compare(const Safe_Array& a, const Safe_Array& b);
Safe_Array& create(unsigned index1, unsigned index2);
private:
int *m_data;
unsigned m_capacity;
static unsigned score;
const int m_max_tries;
unsigned int counter;
};
这是函数的 cpp 文件。(包括干扰项、承包商、复制承包商和函数大小调整):
Safe_Array::Safe_Array(unsigned capacity, int max_tries) :
m_max_tries(max_tries), m_capacity(0), m_data(NULL), counter(0)
{
m_capacity = capacity;
//counter = 0; // when created counter = 0
m_data = new int[capacity];
memset(m_data, 0, m_capacity*sizeof(int));
score += sizeof(Safe_Array) + sizeof(int)*m_capacity;
}
Safe_Array::Safe_Array(const Safe_Array& org_obj) :
m_max_tries(org_obj.m_max_tries), m_capacity(org_obj.m_capacity), counter(0) // copy constractor
{
m_data = new int[m_capacity];
memcpy(m_data, org_obj.m_data, m_capacity * sizeof(int)); // copy sizeof(int)*4 -> int is 4 bytes & memcpy copies bytes
score += sizeof(Safe_Array) + sizeof(int)*m_capacity;
}
Safe_Array::~Safe_Array() // distractor
{
if (m_data) // check if object exists
delete[] m_data;
score -= sizeof(Safe_Array) + sizeof(int)*m_capacity; // uptade score
}
可能Safe_Array temp(*this);
不(深度)复制m_data
,因此删除后您会看到释放的内存。
稍后移动删除:
if (m_data) {
int * old = m_data;
m_data = new int[sizeof(int)*new_capacity]; // new allocation
m_capacity = new_capacity;
for (int i = 0; i < old_len + 1; i++)
m_data[i] = old[i];
delete[] old;
}
克隆当前实例只是为了(希望)保存它的成员是一种不好的方式。
相关文章:
- 为什么我的 heap.h 文件给我一个LNK2001错误?
- 当为可变性配置时,boost::heap::d_ary_heap 保留的额外 std::list 的目的是什么?
- 了解 Linux 虚拟内存:valgrind 的 massif 输出显示了有和没有 --pages-as-heap 的主要差异
- 逻辑'double free or corruption (fasttop)'错误
- emplace_back和push_back给出错误'double free or corruption (fasttop)'尽管定义了复制和移动构造函数
- 从 MSVC14 切换到 MSVC16 会导致"compiler is out of heap space (C1060)"错误
- 庞大的初始化列表,如何修复"fatal error C1060: compiler is out of heap space"
- C++ 调用析构函数后动态模板队列"double free or corruption (out)"
- 为什么这段代码会导致"heap corruption detected"?
- Windows heap corruption C++C
- Heap corruption after new char[strlen
- Corruption Of Heap
- 如何调试我的"heap corruption"?(C++)
- E_OUTOFMEMORY, Heap corruption
- OpenCV findContours - 2.4.5 Heap Corruption
- 释放内存时获得"Heap Corruption Detected: after Normal block"
- Heap corruption C++
- Convex hull & Heap corruption with openCV
- 为什么在这里返回一个 malloc'd 指针会导致免费"HEAP CORRUPTION"?
- C++ Heap Corruption