有人可以帮我弄清楚我在哪里收到此错误吗?我知道这可能是双重删除或类似的事情。对于背景,这是霍夫曼树的实现,您可以轻松地实现维基百科 http://en.wikipedia.org/wiki/Huffman_coding.
CharCountNode 类实现 http://pastebin.com/6CM9eman
int main()
{
ifstream input;
input.open("input.txt");
MinPriorityQueue<CharCountNode> heap;
map<char, int> m;
while(input.good())
m[input.get()] += 1;
for( map<char, int>::const_iterator it = m.begin(); it != m.end(); ++it )
heap.enqueue(CharCountNode(it->first, it->second));
while(heap.getSize() > 1)
{
CharCountNode a, b, parent;
a = heap.dequeue();
b = heap.dequeue();
parent = CharCountNode('*', a.getCount() + b.getCount());
parent.left = &a;
parent.right = &b;
heap.enqueue(parent);
}
}
问题出在这段代码上:
parent.left = &a;
parent.right = &b;
这是获取指向局部变量的指针,该变量将在下次循环时重新初始化。CharCountNode
最终会尝试delete
这些对象,但它们还没有被new分配。
你需要做left
and right
指向堆上分配的对象,因为这就是CharCountNode
正在期待。就像是:
parent.left = new CharCountNode(a);
parent.right = new CharCountNode(b);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)