的malloc:***错误对象:被释放的指针没有被分配在***向malloc_error_break调试设置断点 [英] malloc: *** error for object: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug

查看:917
本文介绍了的malloc:***错误对象:被释放的指针没有被分配在***向malloc_error_break调试设置断点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我找出在那里我得到这个错误。我知道这可能是一个双缺失或类似这样的东西。对于背景,这是哈夫曼树的实现,你可以很容易地在维基百科实现。

CharCountNode类实现

  INT的main()
{
  ifstream的输入;
  input.open(input.txt中);  MinPriorityQueue< CharCountNode>堆;
  地图<焦炭,INT>米;  而(input.good())
    米[input.get()] + = 1;  对于(地图< CHAR,INT和GT; ::为const_iterator吧= m.begin(!);它= m.end(); ++吧)
    heap.enqueue(CharCountNode(IT->首先,它 - >第二个));
  而(heap.getSize()→1)
  {
    CharCountNode的a,b,父母;    一个= heap.dequeue();
    B = heap.dequeue();
    父= CharCountNode('*',a.getCount()+ b.getCount());    parent.left =安培; A;
    parent.right =和b;    heap.enqueue(父);
  }
}


解决方案

现在的问题是这个code:

  parent.left =安培; A;
parent.right =和b;

此是越来越指针局部变量,这将被重新初始化围绕循环下一次。 CharCountNode 最终将尝试删除这些对象,但他们还没有被分配新

您需要让离开右键指向在堆上分配的对象,因为这是 CharCountNode 期待。是这样的:

  parent.left =新CharCountNode(一);
parent.right =新CharCountNode(B);

Can someone help me figure out where I'm getting this error. I know it's probably a double deletion or something like this. For the background this is an implementation of the huffman's tree as you can easily realize on wikipedia.

CharCountNode class implementation

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

解决方案

The problem is with this code:

parent.left = &a;
parent.right = &b;

This is getting pointers to local variables, which will be reinitialized next time around the loop. CharCountNode will eventually try to delete these objects, but they haven't been allocated by new.

You need to make left and right point to objects allocated on the heap, as that is what CharCountNode is expecting. Something like:

parent.left = new CharCountNode(a);
parent.right = new CharCountNode(b);

这篇关于的malloc:***错误对象:被释放的指针没有被分配在***向malloc_error_break调试设置断点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆