检测到堆损坏 C++ 自定义向量
Heap Corruption Detected C++ Custom Vector
嘿,我在 class 中重新整理了一些旧项目,当我重做这个项目时,当在驱动程序中调用我的 clear() 函数时,我不断收到此错误,
检测到堆损坏:在正常块之后(#142)
CRT 检测到应用程序在 HEAP 缓冲区结束后写入内存
这是我的自定义矢量 class
#include "MyVector.h"
//insert header files
#include <iostream>
#include <string>
//setup access to necessary libraries
using namespace std;
MyVector::MyVector()
{
// initialize member data
size = 0;
capacity = 2;
//initialize new array
classArray = new int[capacity];
}
MyVector::MyVector(int maxCapacity)
{
// initialize member data
size = 0;
capacity = maxCapacity;
// initialize new array
classArray = new int[capacity];
}
MyVector::~MyVector()
{
if (classArray != NULL)
{
delete [] classArray;
classArray = NULL;
}
}
int MyVector::getSize()
{
return size;
}
int MyVector::getCapacity()
{
return capacity;
}
void MyVector::clear()
{
// delete the array
delete[] classArray;
// reinitialize the array
capacity = 2;
size = 0;
classArray = new int[capacity];
}
void MyVector::push_back(int n)
{
if (size > capacity)
{
// setup the special case of an array with 0 elements
if (size == 0)
{
clear();
}
else
{
// declare a temporary pointer and allocate a new array
capacity = capacity * 2;
int* tempArray = new int[capacity];
// copy the values from the old array to the temporary array
for (int i = 0; i < size; i++)
{
tempArray[i] = classArray[i];
}
// call the destructor
delete[] classArray;
// assign the classArray pointer to the new array
classArray = tempArray;
}
}
// pushback a new value to the array
classArray[size] = n;
// increment size
size++;
}
int MyVector::at(int n)
{
// check if n is within the bounds of the array
if (n >= size)
{
throw n;
}
// if not return the value of the index requested
else
{
return classArray[n];
}
}
这是我的驱动程序代码,
//insert header files
#include <iostream>
#include <string>
#include "MyVector.h"
//setup access to necessary libraries
using namespace std;
//declare constants
#pragma region Constants
const int TEST_VALUE1 = 21;
const int TEST_VALUE2 = 31;
const int TEST_VALUE3 = 41;
const int MAX = 12;
#pragma endregion
int main()
{
// Create a default vector
MyVector sam;
// push some data into sam
cout << "\nPushing three values into sam";
sam.push_back(TEST_VALUE1);
sam.push_back(TEST_VALUE2);
sam.push_back(TEST_VALUE3);
cout << "\nThe values in sam are: ";
// test for out of bounds condition here
// and test exception
for (int i = 0; i < sam.getSize() + 1; i++)
{
try
{
cout << sam.at(i) << " ";
}
catch (int badIndex)
{
cout << "\nOut of bounds at index " << badIndex << endl;
}
}
cout << "\n--------------\n";
// clear sam and display its size and capacity
sam.clear(); //********ERROR BEING THROWN HERE*********
cout << "\nsam has been cleared.";
cout << "\nSam's size is now " << sam.getSize();
cout << "\nSam's capacity is now " << sam.getCapacity() << endl;
cout << "---------------\n";
// Push 12 values into the vector - it should grow
cout << "\nPush 12 values into sam.";
for (int i = 0; i < MAX; i++)
sam.push_back(i);
cout << "\nSam's size is now " << sam.getSize();
cout << "\nSam's capcacity is now " << sam.getCapacity() << endl;
cout << "---------------\n";
cout << "\nTest to see if contents are correct...";
// display the values in the vector
for (int i = 0; i < sam.getSize(); i++)
{
cout << sam.at(i) << " ";
}
cout << "\n--------------\n";
cout << "\n\nTest Complete...";
cout << endl;
system("PAUSE");
return 0;
}
我来回查看了我的旧项目好几次,但我不明白为什么我在尝试删除某些内容时会收到此错误。我的意思是,当我尝试分配无法分配但无法删除的内容时,听起来通常会发生这种情况?
感谢任何帮助!
您的 push_back
代码可以简化为:
if (size > capacity)
{
// resize
}
classArray[size] = n;
size++;
但请注意,您从 size == 0
和 capacity == 2
开始,然后对 push_back
进行了三次调用。第三个,size == 2
和 capacity == 2
。 size > capacity
仍然是 false
,因此您将写入 classArray[2]
(未调整大小),这是未初始化的内存。这是未定义的行为。
您想勾选 size >= capacity
以调整大小。
请注意,您的 class 还有另一个严重的问题:您未能编写复制构造函数,因此如果您复制它,两个副本都会尝试释放相同的内存。请参见三法则(在 C++11 中更新为五法则)。
嘿,我在 class 中重新整理了一些旧项目,当我重做这个项目时,当在驱动程序中调用我的 clear() 函数时,我不断收到此错误,
检测到堆损坏:在正常块之后(#142) CRT 检测到应用程序在 HEAP 缓冲区结束后写入内存
这是我的自定义矢量 class
#include "MyVector.h"
//insert header files
#include <iostream>
#include <string>
//setup access to necessary libraries
using namespace std;
MyVector::MyVector()
{
// initialize member data
size = 0;
capacity = 2;
//initialize new array
classArray = new int[capacity];
}
MyVector::MyVector(int maxCapacity)
{
// initialize member data
size = 0;
capacity = maxCapacity;
// initialize new array
classArray = new int[capacity];
}
MyVector::~MyVector()
{
if (classArray != NULL)
{
delete [] classArray;
classArray = NULL;
}
}
int MyVector::getSize()
{
return size;
}
int MyVector::getCapacity()
{
return capacity;
}
void MyVector::clear()
{
// delete the array
delete[] classArray;
// reinitialize the array
capacity = 2;
size = 0;
classArray = new int[capacity];
}
void MyVector::push_back(int n)
{
if (size > capacity)
{
// setup the special case of an array with 0 elements
if (size == 0)
{
clear();
}
else
{
// declare a temporary pointer and allocate a new array
capacity = capacity * 2;
int* tempArray = new int[capacity];
// copy the values from the old array to the temporary array
for (int i = 0; i < size; i++)
{
tempArray[i] = classArray[i];
}
// call the destructor
delete[] classArray;
// assign the classArray pointer to the new array
classArray = tempArray;
}
}
// pushback a new value to the array
classArray[size] = n;
// increment size
size++;
}
int MyVector::at(int n)
{
// check if n is within the bounds of the array
if (n >= size)
{
throw n;
}
// if not return the value of the index requested
else
{
return classArray[n];
}
}
这是我的驱动程序代码,
//insert header files
#include <iostream>
#include <string>
#include "MyVector.h"
//setup access to necessary libraries
using namespace std;
//declare constants
#pragma region Constants
const int TEST_VALUE1 = 21;
const int TEST_VALUE2 = 31;
const int TEST_VALUE3 = 41;
const int MAX = 12;
#pragma endregion
int main()
{
// Create a default vector
MyVector sam;
// push some data into sam
cout << "\nPushing three values into sam";
sam.push_back(TEST_VALUE1);
sam.push_back(TEST_VALUE2);
sam.push_back(TEST_VALUE3);
cout << "\nThe values in sam are: ";
// test for out of bounds condition here
// and test exception
for (int i = 0; i < sam.getSize() + 1; i++)
{
try
{
cout << sam.at(i) << " ";
}
catch (int badIndex)
{
cout << "\nOut of bounds at index " << badIndex << endl;
}
}
cout << "\n--------------\n";
// clear sam and display its size and capacity
sam.clear(); //********ERROR BEING THROWN HERE*********
cout << "\nsam has been cleared.";
cout << "\nSam's size is now " << sam.getSize();
cout << "\nSam's capacity is now " << sam.getCapacity() << endl;
cout << "---------------\n";
// Push 12 values into the vector - it should grow
cout << "\nPush 12 values into sam.";
for (int i = 0; i < MAX; i++)
sam.push_back(i);
cout << "\nSam's size is now " << sam.getSize();
cout << "\nSam's capcacity is now " << sam.getCapacity() << endl;
cout << "---------------\n";
cout << "\nTest to see if contents are correct...";
// display the values in the vector
for (int i = 0; i < sam.getSize(); i++)
{
cout << sam.at(i) << " ";
}
cout << "\n--------------\n";
cout << "\n\nTest Complete...";
cout << endl;
system("PAUSE");
return 0;
}
我来回查看了我的旧项目好几次,但我不明白为什么我在尝试删除某些内容时会收到此错误。我的意思是,当我尝试分配无法分配但无法删除的内容时,听起来通常会发生这种情况?
感谢任何帮助!
您的 push_back
代码可以简化为:
if (size > capacity)
{
// resize
}
classArray[size] = n;
size++;
但请注意,您从 size == 0
和 capacity == 2
开始,然后对 push_back
进行了三次调用。第三个,size == 2
和 capacity == 2
。 size > capacity
仍然是 false
,因此您将写入 classArray[2]
(未调整大小),这是未初始化的内存。这是未定义的行为。
您想勾选 size >= capacity
以调整大小。
请注意,您的 class 还有另一个严重的问题:您未能编写复制构造函数,因此如果您复制它,两个副本都会尝试释放相同的内存。请参见三法则(在 C++11 中更新为五法则)。