堆腐蚀检测到C ++自定义矢量

时间:2015-05-05 19:17:32

标签: c++ vector dynamic-memory-allocation

嘿我正在通过我参加的一个班级中的一些旧项目进行重复,因为我正在重做这个项目,当我在驱动程序中调用clear()函数时,我一直收到此错误,

检测到堆腐蚀:正常阻塞后(#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;
}

我几次在我的旧项目中来回查看,当我试图删除某些内容时,我无法理解为什么会出现此错误。我的意思是,当我试图分配一些无法分配但未删除的内容时,通常会发生这种情况?

任何帮助表示感谢!

1 个答案:

答案 0 :(得分:4)

您的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中更新为五条规则)。

相关问题