我有下面的代碼,在那裏我實現了一堆堆。雙重免費或數據結構中的損壞
Queue<Heap<struct infoNode>, SIZE> queue;
for(int c = 0 ; c < SIZE; c++)
{
Heap<struct infoNode> h;
queue.enqueue(h, 0);
}
在一堆,我重載拷貝構造函數是這樣的:
template <typename T>
Heap<T>::Heap(const Heap<T> &h)
{
cout << "this " << this << " h " << &h << endl;
capVect = h.capVect;
if(values) delete [] values;
values = new T[capVect];
dimVect = h.dimVect;
for(int i = 0; i < dimVect; i++)
values[i] = h.values[i];
}
當我做queue.enqueue(h, 0)
,我得到雙重釋放或腐敗。我不明白爲什麼我在這裏看到相同的地址以及要複製的堆。
this 0x7ffcbc2771a0 h 0x7ffcbc277190
this 0x7ffcbc2771a0 h 0x7ffcbc277190
*** Error in `./comp': double free or corruption (top): 0x00000000017f0690 ***
Aborted (core dumped)
你能展示更多'堆'嗎? –
http://stackoverflow.com/help/mcve – melpomene
'if(values)delete [] values;'不應該在構造函數中完成。 – NathanOliver