在析构函数中删除[],在构造函数中分配

时间:2014-07-22 21:39:45

标签: c++ delete-operator

我在构造函数中分配内存,然后在析构函数中删除[],但我得到_Block_Type_Is_Valid (pHead->nBlockUse)" Error。我遇到了 三个 的规则,并且与之相处得很好。在我的程序中,我只有一个实例,我使用的也不是复制构造函数,也不是复制赋值运算符所以我认为我只需要显式声明析构函数。 在构造函数中,我正在复制char数组的值,但我无法理解错误。 这是程序代码的简短列表。 部首:

class CTest
{
private:
    char *TestTable;
    int TestTableLength;
    std::chrono::steady_clock::time_point StartPoint;
    std::chrono::steady_clock::time_point EndPoint;
    std::chrono::steady_clock::time_point CheckPoint;
    std::chrono::system_clock::duration d;
public:
    CTest(char *SignTable);
    ~CTest();
    void NewCombination();
    bool AskToPlay();
    bool AskForNewCombination();
    void Play();
};

来源:

CTest::CTest(char *SignTable)
{
    int SignTableLength = 0;
    for (int i = 0; *(SignTable + i) != '\0'; i++)
    {
        SignTableLength++;
    }
    TestTableLength = SignTableLength ;
    TestTable = new char[TestTableLength];
    for (int i = 0; *(SignTable + i) != '\0'; i++)
    {
        *(TestTable + i) = *(SignTable + i);
    }
}


CTest::~CTest()
{
    delete[] TestTable;
}

void CTest::NewCombination()
{
    int tmpInt; 
    char tmpChar;
    if (TestTable != NULL)
    {
        for (int i = 0; i < TestTableLength - 1; i++)
        {
            tmpInt = rand() % (TestTableLength - i);
            tmpChar = TestTable[TestTableLength - 1 - i];
            TestTable[TestTableLength - 1 - i] = TestTable[tmpInt];
            TestTable[tmpInt] = tmpChar;
        }
    }
}

bool CTest::AskToPlay()
{
    char play;
    std::cout << "Do you want to play again ?" << std::endl << "If yes press 'y' else  press something else" << std::endl;
    std::cin >> play;
    if (play == 'y') return true;
    else return false;
};

bool CTest::AskForNewCombination()
{
    char newcom;
    std::cout << "Do you want me to create new combination?" << std::endl << "If yes press 'y' else press something else" << std::endl;
    std::cin >> newcom;
    if (newcom == 'y') return true;
    else return false;
};

void CTest::Play()
{
    StartPoint = std::chrono::steady_clock::now();
    CheckPoint = std::chrono::steady_clock::now();
    std::cout << "3\t";
    d = CheckPoint - StartPoint;
    while (std::chrono::duration_cast<std::chrono::milliseconds>(d).count() < 1000)
    {
        CheckPoint = std::chrono::steady_clock::now();
        d = CheckPoint - StartPoint;
    }
    std::cout << "2\t";
    while (std::chrono::duration_cast<std::chrono::milliseconds>(d).count() < 2000)
    {
        CheckPoint = std::chrono::steady_clock::now();
        d = CheckPoint - StartPoint;
    }
    std::cout << "1" << std::endl;
    while (std::chrono::duration_cast<std::chrono::milliseconds>(d).count() < 3000)
    {
        CheckPoint = std::chrono::steady_clock::now();
        d = CheckPoint - StartPoint;
    }
    std::cout << "START!" << std::endl;
    StartPoint = std::chrono::steady_clock::now();
    for (int i = 0; i < (TestTableLength ) ; i++)
    {
        std::cout << *(TestTable + i) << " ";
    }
};

主:

char Signs[] = { '1', '2', '3', '4', '5', '6', 'q', 'w', 'e', 'r', 'f', 'g' };
CTest Test(Signs);

int _tmain(int argc, _TCHAR* argv[])
{
    srand(time(NULL));
    while (Test.AskToPlay())
    {
        if (Test.AskForNewCombination()) Test.NewCombination();
        Test.Play();
    }

    Test.~CTest();
    return 0;
}

1 个答案:

答案 0 :(得分:2)

您在main结尾处明确调用析构函数。不应该这样做,在程序结束时自动调用析构函数(即当你的全局Test对象超出范围时)。您明确调用的析构函数释放已分配的内存,并在程序终止时再次调用析构函数,尝试再次释放内存,导致观察到的失败。