自定义链表创建 RtlValidateHeap 错误,其结构具有链表
custom linked list creating RtlValidateHeap error with the structures having a Linked list
这是我正在做的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++:如何做一个链表,结构作为参数传递?
- 为什么将函数的返回类型从结构节点*更改为void后,链表的元素没有显示create_ll和显示?
- 如何通过不同的指针使用类的对象访问结构?(链表)(C++)
- 在C++中使用链表的堆栈实现中,访问结构体headNode成员count和top会导致运行时错误
- 在排序链表中插入结构并将其写入二进制文件
- 具有多个结构的链表
- 使用带有结构结构作为 C++; 元素的单向链表
- 如何从文件读取到动态分配的指向结构的指针链表中?
- 结构内具有更多数据类型的单个链表
- 尝试连接两种不同类型的结构来创建链表
- C++中的数据结构,插入链表中节点的开头
- 链表结构(将结构指针传递给函数)
- 使用链表结构 C++ 将值推送到堆栈类
- 如何在C++中将链表结构返回到main
- 链表结构推送和弹出
- 删除链表结构的链表