当我尝试运行代码时,标题中出现错误... 这就是我正在运行的
class MyQueue {
tile* queue;
int size;
public:
MyQueue(int cap);
~MyQueue();
void enqueue(tile t);
tile dequeue();
bool isEmpty();
};
void MyQueue::enqueue(tile t) {
queue[size] = t;
size++;
}
tile MyQueue::dequeue() {
tile temp = queue[0];
tile* victim = queue;
queue++;
delete victim;
return temp;
}
bool MyQueue::isEmpty() {
if (size == 0)
return true;
else
return false;
}
MyQueue::MyQueue(int cap) {
queue = new tile[cap];
size = 0;
}
MyQueue::~MyQueue() {
delete[] queue;
}
int main(int argc, char *argv[]) {
tile tile1; tile1.type = '1';
tile tile2; tile2.type = '2';
tile tile3;
MyQueue q(10);
q.enqueue(tile1);
q.enqueue(tile2);
tile3 = q.dequeue();
cout<<tile3.type<<" =1?"<<endl;
return 0;
}
的valgrind:
==4506== Mismatched free() / delete / delete []
==4506== at 0x4C27FFF: operator delete(void*) (vg_replace_malloc.c:387)
==4506== by 0x400CD2: MyQueue::dequeue() (MyQueue.h:37)
==4506== by 0x400DD7: main (p1.cpp:24)
==4506== Address 0x5964040 is 0 bytes inside a block of size 120 alloc'd
==4506== at 0x4C28658: operator new[](unsigned long) (vg_replace_malloc.c:305)
==4506== by 0x400D35: MyQueue::MyQueue(int) (MyQueue.h:49)
==4506== by 0x400D9F: main (p1.cpp:21)
==4506==
1 =1?
==4506== Invalid free() / delete / delete[]
==4506== at 0x4C27C7B: operator delete[](void*) (vg_replace_malloc.c:409)
==4506== by 0x400D74: MyQueue::~MyQueue() (MyQueue.h:54)
==4506== by 0x400E2F: main (p1.cpp:21)
==4506== Address 0x596404c is 12 bytes inside a block of size 120 free'd
==4506== at 0x4C27FFF: operator delete(void*) (vg_replace_malloc.c:387)
==4506== by 0x400CD2: MyQueue::dequeue() (MyQueue.h:37)
==4506== by 0x400DD7: main (p1.cpp:24)
==4506==
==4506==
==4506== HEAP SUMMARY:
==4506== in use at exit: 0 bytes in 0 blocks
==4506== total heap usage: 1 allocs, 2 frees, 120 bytes allocated
==4506==
==4506== All heap blocks were freed -- no leaks are possible
==4506==
==4506== For counts of detected and suppressed errors, rerun with: -v
==4506== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 4 from 4)
看起来像是愚蠢的东西,但我无法弄清楚我的生活,谢谢你的帮助
答案 0 :(得分:2)
这是可疑的:
tile MyQueue::dequeue()
{
tile temp = queue[0];
tile* victim = queue;
queue++; // <----- Note this!
delete victim;
return temp;
}
调用此函数将递增queue
指针。这会导致您从new[]
获得的指针与传递给delete[]
的指针不同。传递指向delete[]
的指针与new[]
返回的指针不同会产生未定义的行为。
更不用说您尝试使用tile
行删除单个delete victim
。您无法删除分配有new[]
的数组中的单个项目,删除项目的唯一方法是一次删除整个数组。
int main(int argc, char *argv[])
{
tile tile1; tile1.type = '1';
tile tile2; tile2.type = '2';
tile tile3;
MyQueue q(10); // calls new[] and assigns pointer to queue.
q.enqueue(tile1);
q.enqueue(tile2);
tile3 = q.dequeue(); // **increments queue**
cout<<tile3.type<<" =1?"<<endl;
return 0;
// When MyQueue is destroyed, its destructor passes new value of
// queue to delete[], which of course doesn't work.
}
除非这是作业,否则你应该使用std::queue
代替。它经过了全面测试,是C ++标准库的一部分。另外,请选择a good introductory C++ book。你的问题表明了对内存管理工作原理的一个基本误解。
答案 1 :(得分:2)
问题可能是由于在构造函数中你正在动态分配一个数组:
queue = new tile[cap];
但在deque
中使用非数组删除运算符删除它。
tile* victim = queue;
queue++;
delete victim;
看起来代码中的逻辑不匹配。一方面,有代码分配动态数组,但在其他地方删除动态分配数组的各个元素。
答案 2 :(得分:1)
您没有分配您推送的原始图块,因此您不应在此函数中解除分配。你已经在析构函数中处理了总数组释放,这很好。
tile MyQueue::dequeue()
{
assert( size != 0 ); // could throw an exception here
tile temp = queue[size-1];
--size;
return temp;
}
同样在enqueue中你不会检查你是否会添加一个超过你分配的数组大小的元素,所以应该把另一个断言/异常放在那里。