我用初始化列表给指针开堆区空间,在释放空间的时候VS报错,报错如下:


我的代码如下:
InstructionQueue::InstructionQueue(unsigned char size): size(size + 1), front(0), rear(0), instruction(new char[this->size])
{
}
InstructionQueue::~InstructionQueue()
{
delete[] instruction;
if (instruction)
{
instruction = nullptr;
}
}
我使用的是VS2022。
我有尝试过寻找原因:
1.不是重复释放空间问题,因为我写了深拷贝构造,重复释放是另一种报错提示。
2.就报错是出在delete[]那一句,因为注释掉就正常运行了。但是问题是出在初始化列表,因为把初始化列表改成构造函数内开空间,就不会出现这个问题。
3.如果不使用初始化列表,在构造函数内开空间也没有报错:
InstructionQueue::InstructionQueue(unsigned char size): size(size + 1), front(0), rear(0)
{
instruction = new char[this->size];
}
4.我尝试问gpt,gpt说没有任何问题,提供的推荐方法也都无法解决这个问题。
请赐教,万分感激!