所以我正在编写一个有n个孩子的 Node 模板类。 我的问题是在构造函数中。似乎当我想为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;
}
谢谢你
答案 0 :(得分: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,