gpt4 book ai didi

c++ - 检测到堆损坏 C++ 自定义 vector

转载 作者:行者123 更新时间:2023-11-27 22:58:35 25 4
gpt4 key购买 nike

嘿,我正在重温我参加的类(class)中的一些旧项目,并且在重做这个项目时,当在驱动程序中调用我的 clear() 函数时,我不断收到此错误,

检测到堆损坏:在正常 block 之后(#142)CRT 检测到应用程序在 HEAP 缓冲区结束后写入内存

这是我自定义的 Vector 类

#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 == 0capacity == 2 开始,然后对 push_back 进行了三次调用。在第三个上,size == 2capacity == 2size > capacity 仍然是 false,因此您将写入未初始化内存的 classArray[2](无需调整大小)。这是未定义的行为。

您想检查 size >= capacity 以调整大小。

请注意,您的类还有另一个严重的问题:您未能编写复制构造函数,因此如果您复制它,两个拷贝都会尝试释放相同的内存。请参见三法则(在 C++11 中更新为五法则)。

关于c++ - 检测到堆损坏 C++ 自定义 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30061428/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com