gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 02:44:27 28 4
gpt4 key购买 nike

我在构造函数中分配内存,然后在析构函数中执行 delete[],但我得到了 _Block_Type_Is_Valid (pHead->nBlockUse)"错误。我遇到了三法则 并接受了。在我的程序中,我只有一个实例,我既没有使用复制构造函数,也没有使用复制赋值运算符,所以我认为我只需要显式声明的析构函数。在构造函数中,我正在复制 char 数组的值,但我不知道出了什么问题。这是程序代码的简短列表。 header :

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;
}

最佳答案

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

关于c++ - 在析构函数中删除[],在构造函数中分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24898336/

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