删除[]数组会破坏我的C ++程序

时间:2015-03-28 23:03:29

标签: c++ dynamic allocation

我已经有这个问题了一段时间了。每次我的grow函数调用删除行时,程序都会中断。除了达到断点之外,它没有给我一个错误。我在谷歌搜索期间没有找到这个在线解决方案。有人可以帮忙吗? 更新 如果最终出现了不同的错误,请在错误屏幕上按几次继续。它声明了CrtIsValidHeapPointer(pUserData)

Driver.cpp

#include <iostream>
#include "MyVector.h"     
using namespace std;

// the printV function
// used to test the copy constructor
// parameter: a MyVector object
void printV(MyVector);

int main()
{
    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");
    return 0;
}

void printV(MyVector v)
{
    cout << "\n--------------------\n";
    cout << "Printing a copy of a vector\n";
    cout << v;
}

我的Vector.h

#include <iostream>
#include <ostream>
using namespace std;
#pragma once


class MyVector
{
private:
    int vSize;
    int vCapacity;
    int* vArray;
    void grow();

public:
    MyVector();
    MyVector(int n);
    MyVector(const MyVector& b);
    int size() const;
    int capacity() const;
    void clear();
    void push_back(int n);
    int& at(int n) const;
    MyVector& operator=(const MyVector& rho);
    ~MyVector();
};

ostream& operator<<(ostream& out, const MyVector& rho);

MyVector.cpp

#include "MyVector.h"
#include <iostream>
#include <ostream>
using namespace std;


MyVector::MyVector()
{
    vArray = nullptr;
    vSize = 0;
    vCapacity = 0;
}

MyVector::MyVector(int n)
{
    vArray = new int[vCapacity];
    vSize = 0;
    vCapacity = n;
}

MyVector::MyVector(const MyVector& b)
{
    vSize = b.size();
    vArray = new int[vSize];
    for (int i = 0; i < b.size(); i++)
    {
        this->vArray[i] = b.vArray[i];
    }
}

int MyVector::size() const
{
    return vSize;
}

int MyVector::capacity() const
{
    return vCapacity;
}

void MyVector::clear(void)
{
    if (vArray != nullptr)
    {
        delete[] vArray;
        vArray = nullptr;
        vSize = 0;
        vCapacity = 0;
    }
}

void MyVector::push_back(int n)
{
    if (vCapacity == 0)
    {
        vCapacity++;
        int* tmp = new int[vCapacity];
        delete[] vArray;
        vArray = tmp;
    }

    if (vSize >= vCapacity)
    {
        grow();
    }
    vArray[vSize] = n;
    vSize++;
}

void MyVector::grow()
{
    vCapacity = vCapacity + vCapacity;
    int* tmp = new int[vCapacity];
    for (int i = 0; i < vSize+1; i++)
    {
        tmp[i] = vArray[i];
    }
    delete[] vArray;
    vArray = tmp;
}

int& MyVector::at(int index) const
{
    if (index >= 0 && index<vSize)
    {
        return vArray[index];
    }
    else
    {
        throw index;
    }
}

MyVector& MyVector::operator=(const MyVector& rho)
{
    // test for self assignment
    if (this == &rho)
        return *this;

    // clean up array in left hand object (this)
    delete[] this->vArray;

    // create a new array big enough to hold right hand object's data
    vSize = rho.size();
    this->vArray = new int[vSize];

    // copy the data
    for (int i = 0; i < rho.size(); i++)
    {
        this->vArray[i] = rho.vArray[i];
    }

    // return this object
    return *this;
}

MyVector::~MyVector()
{
    if (vArray != nullptr)
    {
        clear();
    }
}

ostream& operator<< (ostream& out, const MyVector& rho)
{
    for (int i = 0; i < rho.size(); i++)
    {
        out << rho.at(i);
    }
    return out;
}

1 个答案:

答案 0 :(得分:1)

你的问题是你的构造函数带有一个参数:

MyVector::MyVector(int n)
{
    vArray = new int[vCapacity];
    vSize = 0;
    vCapacity = n;
}

在为其分配值之前,您正在使用vCapacity。这可能导致尝试分配大量或不足够大的数据块。