相关疑难解决方法(0)

何时使用"new"和何时不使用,在C++中?

可能重复:
我何时应该在C++中使用new关键字?

我什么时候应该在C++中使用"new"运算符?我来自C#/ Java背景,实例化对象让我感到困惑.

如果我创建了一个名为"Point"的简单类,当我创建一个点时,我应该:

Point p1 = Point(0,0);
Run Code Online (Sandbox Code Playgroud)

要么

Point* p1 = new Point(0, 0);
Run Code Online (Sandbox Code Playgroud)

有人可以为我澄清何时使用新操作员以及何时不使用?

重复:

我什么时候应该在C++中使用new关键字?

有关:

关于C++中用于自定义对象的构造函数/析构函数和new/delete运算符

在C++中适当的堆栈和堆使用?

c++ new-operator

98
推荐指数
2
解决办法
11万
查看次数

malloc:***对象的错误:没有分配被释放的指针***在malloc_error_break中设置一个断点来调试

有人可以帮我弄清楚我在哪里得到这个错误.我知道它可能是双重删除或类似的东西.对于背景,这是霍夫曼树的一个实现,你可以在维基百科上轻松实现.

CharCountNode类实现

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);
  }
}
Run Code Online (Sandbox Code Playgroud)

c c++ malloc pointers huffman-code

14
推荐指数
1
解决办法
4万
查看次数

标签 统计

c++ ×2

c ×1

huffman-code ×1

malloc ×1

new-operator ×1

pointers ×1