树 class 具有 children 的模板和动态分配

Tree class with template and dynamic allocation for children

所以我正在写一个 Node 模板 class 和 n children。 我的问题出在构造函数中。貌似当我想为Node类型的数组分配内存时(在Node(V data)构造函数中),即使我使用new也没有分配内存。


正如您在这张内存状态图像中所见,堆上没有任何内容。 Memory state

这是我的代码。

template <typename V>
class Node {
private:
  V _data;
  unsigned short _size;
  Node<V>* _children;
public:
  Node();
  Node(V, unsigned short);
  Node(const Node&); // copy constructor
  Node& operator= (Node&); // assignement by copy constructor
  Node (Node&&); // transfer constructor
  Node& operator= (Node&&); // assignement by transfer constructor
  ~Node();
};

template <typename V>
Node<V>::Node()
  : _data(0), _size(0), _children(nullptr) {}

template <typename V>
Node<V>::Node(V data, unsigned short size)
  : _data(data), _size(size), _children(new Node<V>[_size]) {}

template <typename V>
Node<V>::Node (const Node& other)
  : _size(other._size), _data(other._data) {
  _children = new Node<V>[_size];
  for (unsigned short i = 0; i < _size; i++) 
    _children[i] = other._children[i];
}

template <typename V>
Node<V>& Node<V>::operator= (Node& n){
  if (&n != this) {
    delete[] _children;
    _data = n._data; _children = n._children; _size = n._size;
    n._data = 0; n._size = 0; n._children = nullptr;
  }
  return *this;
}

template <typename V>
Node<V>::Node (Node&& n){
  _data = n._data; _size = n._size; _children = n._children;
  n._data = 0; n._size = 0; n._children = nullptr;
}

template <typename V>
Node<V>& Node<V>::operator= (Node&& n){
  if (&n != this) {
    delete[] _children;
    _data = n._data; _children = n._children; _size = n._size;
    n._data = 0; n._size = 0; n._children = nullptr;
  }
  return *this; 
}

template <typename V>
Node<V>::~Node() { delete[] _children; }



int main() {
  Node<char> n1('A',5);
  return 0;
}

提前致谢

(很抱歉发帖作为回答,但评论有字数限制)

这是我实际单步执行调试器时所看到的。它像你预期的那样运行,为 _children 分配一个指针,然后初始化 space 中的 5 个节点对象。基于此,我认为您使用的可视化工具存在错误。

Temporary breakpoint 1, main () at ex.cpp:64
64      int main() {
(gdb) step
65                Node<char> n1('A',5);
(gdb) step
Node<char>::Node (this=0x7ffffffedc90, data=65 'A', size=5) at ex.cpp:22
22                Node<V>::Node(V data, unsigned short size)
(gdb) step
23                : _data(data), _size(size), _children(new Node<V>[_size]) {}
(gdb) p data
 = 65 'A'
(gdb) p size
 = 5
(gdb) step
Node<char>::Node (this=0x8413e78) at ex.cpp:19
19                : _data(0), _size(0), _children(nullptr) {}
(gdb) step
Node<char>::Node (this=0x8413e88) at ex.cpp:19
19                : _data(0), _size(0), _children(nullptr) {}
(gdb) step
Node<char>::Node (this=0x8413e98) at ex.cpp:19
19                : _data(0), _size(0), _children(nullptr) {}
(gdb) step
Node<char>::Node (this=0x8413ea8) at ex.cpp:19
19                : _data(0), _size(0), _children(nullptr) {}
(gdb) step
Node<char>::Node (this=0x8413eb8) at ex.cpp:19
19                : _data(0), _size(0), _children(nullptr) {}
(gdb) step
Node<char>::~Node (this=0x7ffffffedc90, __in_chrg=<optimized out>) at ex.cpp:60
60      Node<V>::~Node() { delete[] _children; }
(gdb) p _children
 = (Node<char> *) 0x8413e78
(gdb) step
Node<char>::~Node (this=0x8413eb8, __in_chrg=<optimized out>) at ex.cpp:60
60      Node<V>::~Node() { delete[] _children; }
(gdb) step
0x00007fffff1013c0 in operator delete[](void*, unsigned long) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
(gdb) step
Single stepping until exit from function _ZdaPvm,