具有模板并为孩子动态分配的树类

问题描述 投票:0回答:1

所以我正在编写一个有n个孩子的Node模板类。我的问题是在构造函数中。似乎当我想为Node类型的数组分配内存(在Node(V数据)构造函数中)时,即使我使用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;
}

感谢您的参与

c++ templates new-operator allocation
1个回答
0
投票

(很抱歉,作为答案发布,但注释中有字符限制)

这是我实际调试程序时看到的。它按预期运行,为_children分配了一个指针,然后初始化该空间中的5个Node对象。基于此,我认为您使用的可视化工具存在错误。

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
$1 = 65 'A'
(gdb) p size
$2 = 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
$3 = (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,
© www.soinside.com 2019 - 2024. All rights reserved.