调试断言失败 BLOCK_TYPE_IS_VALID (pHead->nblockuse) 从解构函数
Debug assertion failed BLOCK_TYPE_IS_VALID(pHead->nblockuse) from Deconstructor
我现在很迷茫。 我做了一个向量类。 一切都按照我希望的方式工作,直到最后。 调用析构函数时,我收到一条错误消息:调试断言失败BLOCK_TYPE_IS_VALID(pHead->nblockuse)。 我在SO上看到过很多这样的问题,但是我尝试过的方法都没有奏效。
.h. 的一部分
private:
int* _myArray;
int _size;
int _capacity;
#include "File.h"
const string RETURN_CARRIAGE_STR = "n";
const string SIZE_STR = "Size ";
const string CAPACITY_STR = "Capacity ";
const int INITIAL_CAPACITY = 2;
int main(void)
{
cout << "nCreating a vector Sam of size 4.";
MyVector sam( 4 );
cout << "nPush 12 values into the vector.";
for (int i = 0; i < 12; i++)
sam.push_back(i);
cout << "nHere is sam: ";
cout << sam;
cout << "n---------------n";
cout << "nCreating a vector Joe of size 4.";
MyVector joe( 4 );
cout << "nPush 6 values into the vector.";
for (int i = 0; i < 6; i++)
joe.push_back(i * 3);
cout << "nHere is joe: ";
cout << joe;
cout << "n---------------n";
cout << "nTest the overloaded assignment operator "joe = sam": ";
joe = sam;
cout << "nHere is sam: ";
cout << sam;
cout << "n---------------n";
cout << "nHere is joe: ";
cout << joe;
cout << "n---------------n";
// pass a copy of sam by value
PrintV(sam);
cout << endl;
system("PAUSE");
}
void PrintV(MyVector v)
{
cout << "n--------------------n";
cout << "Printing a copy of a vectorn";
cout << v;
}
// Default Constructor
MyVector::MyVector()
{
_myArray = new int[INITIAL_CAPACITY];
_size = 0;
_capacity = INITIAL_CAPACITY;
//cout << "Default Constructor" << endl;
}
MyVector::MyVector(int aSize)
{
_myArray = new int[aSize];
_size = 0;
_capacity = aSize;
//cout << "Parameterized Constructor" << endl;
}
MyVector::~MyVector()
{
if(_myArray != NULL)
{
delete[] this->_myArray; // --------------This is where I get an error
this->_myArray = NULL;
}
//cout << "Destructor" << endl;
}
int MyVector::GetSize()
{
return _size;
//cout << " size";
}
int MyVector::GetCapacity()
{
return _capacity;
//cout << _capacity << " capacity" << endl;
}
void MyVector::Clear()
{
int* resize_arr = new int[INITIAL_CAPACITY];
delete[] _myArray;
_myArray = resize_arr;
_capacity = INITIAL_CAPACITY - 1;
_size = 0;
}
void MyVector::push_back(int newValue)
{
if(_size < _capacity)
{
_myArray[_size] = newValue;
}
else {
int* resize_arr = new int[_capacity*2];
for(int i = 0; i <= _size; i++)
resize_arr[i] = _myArray[i];
resize_arr[_size] = newValue;
delete[] _myArray;
_myArray = resize_arr;
_capacity = _capacity * 2;
}
_size++;
//cout << _size << " size" << endl;
}
int MyVector::at(int idx)
{
return _myArray[idx];
//cout << _myArray[idx] << " value at index" << endl;
}
ostream& operator<<(ostream& os, MyVector& vec)
{
ostringstream convert;
ostringstream convertCap;
convert << vec.GetSize();
string sizeConverted = convert.str();
convertCap << vec.GetCapacity();
string capConverted = convertCap.str();
string firstTemp = "";
firstTemp = SIZE_STR + sizeConverted + RETURN_CARRIAGE_STR + CAPACITY_STR + capConverted + RETURN_CARRIAGE_STR;
for(int idx = 0; idx < vec.GetSize(); idx++)
{
string secondTemp = "";
ostringstream convertSize;
convertSize << vec.at(idx);
string vecAtIdx = convertSize.str();
secondTemp = vecAtIdx + RETURN_CARRIAGE_STR;
cout << secondTemp;
}
os << firstTemp;
return os;
}
MyVector& MyVector::operator=(MyVector& setterVect)
{
delete [] _myArray;
//MyVector* mPtr = new MyVector();
_myArray = setterVect._myArray;
_capacity = setterVect._capacity;
_size = setterVect._size;
return *this;
}
_myArray = setterVect._myArray;
复制赋值运算符已损坏。在该运算符之后,两个实例具有相同的 _myArray
值。因此,一旦一个被破坏,另一个就会留下一个指向不再存在的东西的指针。
而这个故事的寓意——用std::vector
。
该错误意味着堆损坏。 有很多方法可以破坏堆。就像大卫上面解释的那样,释放一大块内存然后写入它是一种方式。
大多数堆在内存块之前和之后存储一些字节的簿记信息。 如果代码行为异常并更改堆数据,则会收到此类错误。
相关文章:
- EASTL矢量<向量<int>>连续的
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- C 字符串比较“祝您好运”&gt;“再见”
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- C :对矢量进行排序&lt; struct&gt;(结构有2个整数)基于结构的整数之一
- C 操作员&gt;&gt;与突变器过载
- 明确的专业化“ CheckIntmap&lt;&gt;”实例化
- 是否需要使用 - &gt;运算符在C 中调用成员函数时
- 什么是模板&lt;&gt;inline bla bla
- 编辑C Qlist&lt; object*&gt; gt;QML代码和一些QML警告中的模型
- eigen :: llt&lt;eigen :: matrixxd&gt;具有不完整的类型
- 错误,包括&lt; ctype&gt;在原子上使用C 11
- 错误c++visual studio c2227左侧'->;Init';必须指向类/结构/联合/泛型类型
- std::vector<;uint8_t>;当C++11/14启用时,手动复制而不是调用memcpy
- ``这个''不能用this-&gt;指针变量
- 如何加入向量&lt; int&gt;到C 中的单个INT
- 是std :: set&lt; std :: future&gt;不可能存在
- 调试断言失败 BLOCK_TYPE_IS_VALID (pHead->nblockuse) 从解构函数
- 使用shared_ptr<字符串>转换为一个无序集合<字符串>
- 是numeric_limits&lt; int&gt; :: is_modulo从逻辑上矛盾