project.exe在系统之后触发了断点("暂停")

时间:2015-03-29 00:32:17

标签: c++ breakpoints

我已经创建了自己的矢量,当我尝试关闭驱动程序中的控制台时,我按下了Enter并获得了断点。

#include "MyVector.h"
#include <vector>

// Default constructor
MyVector::MyVector()
{
    theData = nullptr;
    vecSize = 0;
    vecCap = 0;
}

// Parameterized constructor
MyVector::MyVector(int vecCap)
{
    // Set vecSize to 0 and vecCap to user input (driver) plus 1 to account
    // for null terminator
    vecSize = 0;
    this->vecCap = vecCap;
    theData = new int[vecCap];
}

// Destructor
MyVector::~MyVector()
{
    // Run the clear() function
    clear();
}

// Copies the vector
void MyVector::copy(const MyVector& toCopy)
{
    // Set size and capacity to toCopy's
    this->vecCap = toCopy.vecCap;
    this->vecSize = toCopy.vecSize;

    // Create a temporary pointer array of ints
    int* tempArray = new int[];

    // Copy data from toCopy to new array
    for (int i = 0; i < toCopy.size(); i++)
    {
        tempArray[i] = toCopy.theData[i];
    }

    // Point theData to the tempArray
    theData = tempArray;
}

// Clears theData and resets vecCap and vecSize to 0
void MyVector::clear()
{
    // Check if theData is null
    if (theData != nullptr)
    {
        // Delete theData from heap and set to nullptr
        delete[] theData;
        theData = nullptr;

        // Set vecSize and vecCap to 0
        vecSize = 0;
        vecCap = 0;
    }
}

// Returns size of the vector
int MyVector::size() const
{
    return vecSize;
}

// Returns capacity of the vector
int MyVector::capacity() const
{
    return vecCap;
}

// Push input values into vector
void MyVector::push_back(int n)
{
    // Check if vecSize is too big for vecCap
    if (vecSize >= vecCap)
    {
        // Double vecCap through grow() function
        grow(vecCap);
    }

    // Set theData at element vecSize to user input n, increment vecSize
    theData[vecSize] = n;
    vecSize++;
}

// Returns index value of vector
int MyVector::at(int vecIdx) const
{
    // Check if vecIdx is within bounds
    if (vecIdx >= 0 && vecIdx <= vecSize)
    {
        // Return vector index
        return theData[vecIdx];
    }
    else
    {
        // Display out of bounds index
        throw vecIdx;
    }
}

// Doubles the size of the vector capacity
void MyVector::grow(int curCap)
{
    // Check if curCap is 0 ro less
    if (curCap <= 0)
    {
        // Set vecCap to CAP_GROWTH -1
        vecCap = CAP_GROWTH - 1;
    }
    else
    {
        // Increase curCap by CAP_GROWTH (doubled)
        vecCap = CAP_GROWTH * curCap;
    }

    // Create new array
    int* newArray = new int[vecCap];

    // Copy data to new array
    for (int idx = 0; idx < vecSize; idx++)
    {
        newArray[idx] = theData[idx];
    }

    // Delete theData
    delete[] theData;

    // Point theData to new array
    theData = newArray;
}

//
MyVector& MyVector::operator=(const MyVector& rho)
{
    // Check if the implicit object's address is the same as rho
    if (this != &rho)
    {
        // Clear the implicit object
        this->clear();
        // Copy the 
        this->copy(rho);
    }
    return *this;
}

// 
ostream& operator<<(ostream& out, const MyVector& rho)
{
    for (int idx = 0; idx < rho.size(); idx++)
    {
        // Output index of rho, separated by a space
        out << rho.at(idx) << " ";
    }
    return out;
}

我已经检查了某个地方,我可能试图重新删除指针,但我找不到任何东西,任何它都没有说出为什么抛出异常。有什么提示吗?

2 个答案:

答案 0 :(得分:1)

你的班级不遵守3的规则:

What is The Rule of Three?

您缺少复制构造函数。您的copy函数不是复制构造函数。您应该首先实现适当的复制构造函数。

此外,您的copy函数存在内存泄漏,因为您从未删除旧数据。

以下是复制构造函数的示例

#include <algorithm>
//...
MyVector::MyVector(const MyVector& toCopy) : vecCap(toCopy.vecCap),
                                             vecSize(toCopy.vecSize),
                                             theData(new int[toCopy.capacity()])
{
    std::copy(toCopy.theData, toCopy.theData + toCopy.size(), theData);       
}

现在,如果你真的想保留copy函数(你真的不需要它,但为了论证你想保留它),你可以使用上面的复制构造函数:

// Copies the vector
void MyVector::copy(const MyVector& toCopy)
{
    MyVector temp(toCopy);  // calls the copy constructor
    std::swap(temp.vecCap, vecCap);
    std::swap(temp.vecSize, vecSize);
    std::swap(temp.theData, theData);
}

请注意,我们所做的只是创建一个临时副本,并将临时副本的数据与当前数据交换出来。当函数返回时,临时消失,并带有旧数据。

最后,您的赋值运算符可以使用copy函数编写。

MyVector& MyVector::operator=(const MyVector& rho)
{
    copy(rho);
    return *this;
}

所以从某种意义上说,如果唯一的目的是移动一些代码,那么copy函数就有用了。

实际上,即使没有我建议的更改,这也是您应该为您的赋值运算符编码的内容。所有赋值运算符应该一直在调用copy并返回当前对象。

答案 1 :(得分:0)

在复制功能中,清除旧数据,根据容量创建新数据:

void MyVector::copy(const MyVector& x)
{
    clear();
    vecCap = x.vecCap;
    vecSize = x.vecSize;
    theData = new int[vecCap];

    for (int i = 0; i < x.size(); i++)
        theData[i] = x.theData[i];
}

你也在论证中使用相同的名字,你的方式并没有错,但使用不同的名字更容易:

MyVector::MyVector(int c)
{
    vecSize = 0;
    vecCap = c;
    theData = new int[vecCap];
}