gpt4 book ai didi

c++ - 自定义链表使用具有链表的结构创建 RtlValidateHeap 错误

转载 作者:太空宇宙 更新时间:2023-11-04 13:00:11 26 4
gpt4 key购买 nike

这是我正在处理的 LinkedList 实现。它适用于任何数据类型,但是当我尝试制作具有链接列表的类型的链接列表时出现问题在使用 visual studio 进行调试时,我得到了指定给 RtlValidateHeap 的无效地址( 00790000, 007B16D0 )

代码如下:

typedef unsigned long int LENGTH_T;
template < typename type >
struct nodes
{
type _value;
nodes<type> * _next_node;
nodes() { _next_node = nullptr; }
};

template < typename type >
class LinkedList
{
nodes<type> * _elem_nodes;
LENGTH_T _size;
public:
nodes<type> * node_at(LENGTH_T at);
type& operator[] (LENGTH_T at);
void push_back(const type src);
LENGTH_T size() const { return _size; }
LinkedList();
~LinkedList();
};

template<typename type>
nodes<type>* LinkedList<type>::node_at(LENGTH_T at) {
if (at == 0)
return _elem_nodes;
else if (at > _size - 1 || _size == 0) {
PRINT_ERROR("try to access out of range");
}

// tmp node for storing sequential nodes
nodes<type> * cur_tmp_node_ptr = _elem_nodes->_next_node;

for (size_t i = 1; i < at; i++)
cur_tmp_node_ptr = cur_tmp_node_ptr->_next_node;

return cur_tmp_node_ptr;
}

template<typename type>
type & LinkedList<type>::operator[](LENGTH_T at)
{
return node_at(at)->_value;
}

template<typename type>
void LinkedList<type>::push_back(const type src)
{
if (_size == 0) {
_elem_nodes->_value = src;
_size++;
}
else {
nodes<type> * new_node = new nodes<type> ;
new_node->_value = src;
new_node->_next_node = nullptr;
node_at(_size - 1)->_next_node = new_node;
_size++;
}
}

template<typename type>
LinkedList<type>::LinkedList()
{
_size = 0;
_elem_nodes = new nodes<type>;
_elem_nodes->_value = type();
_elem_nodes->_next_node = nullptr;
}

template<typename type>
LinkedList<type>::~LinkedList()
{
if (_size > 1) // When size = 0 , so _size-1 = -1 but _size is unsigned;
for (LENGTH_T i = _size - 1; i > 0; i--) {
delete ( node_at(i) );
}
delete ( _elem_nodes );
}

这里是一个代码示例,其中可以观察到指定的问题f.e

struct test {
int anything;
};

struct test2 {
LinkedList<test> t;
};

int main()
{
LinkedList<test2> t;
t.push_back(test2());
t.push_back(test2());
return 0;
}

**编辑:我编写了自定义赋值运算符和复制构造函数并且不再出现该错误但是在上面的示例中 test2().t._next_node 总是包含垃圾值而不是 nullptr 我不明白为什么 **

template<typename type>
LinkedList<type>& LinkedList<type>::operator=(const LinkedList<type>& other )
{
if (&other == this)
return *this;
this->~LinkedList();
this->_elem_nodes = nullptr;
_size = 0;
nodes<type> * cur_this_node = this->_elem_nodes;
nodes<type> * cur_other_node = other._elem_nodes;
while (cur_other_node != nullptr)
{
cur_this_node = new nodes<type>;
cur_this_node->_value = cur_other_node->_value;
this->_size++;
cur_this_node = cur_this_node->_next_node;
cur_other_node = cur_other_node->_next_node;
}
return *this;
}

template<typename type>
LinkedList<type>::LinkedList(const LinkedList<type>& other)
{
_size = 0;
nodes<type> * cur_this_node = this->_elem_nodes;
cur_this_node = nullptr;
nodes<type> * cur_other_node = other._elem_nodes;
while (cur_other_node != nullptr)
{
cur_this_node = new nodes<type>;
cur_this_node->_value = cur_other_node->_value;
cur_this_node->_next_node = nullptr;
this->_size++;
cur_this_node = cur_this_node->_next_node;
cur_other_node = cur_other_node->_next_node;
}
}

最佳答案

您违反了三(或四或五)条规则。您已经定义了自定义析构函数,但没有定义自定义赋值运算符。在您的情况下,这最终会导致两个单独的 LinkedList 对象指向相同的节点。

更多信息:https://stackoverflow.com/a/4782927/951890

关于c++ - 自定义链表使用具有链表的结构创建 RtlValidateHeap 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44474590/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com