[英]How can I delete the Error when creating pointers on C++? I this case I have to use raw pointers
I'm trying to make a linked list with a pointer to a template class called Node:我正在尝试使用指向名为 Node 的模板类的指针创建一个链表:
template <typename T>
class llnode
{
public:
T key;
llnode<T> *prev, *next;
llnode()
{
};
llnode(const T &k, llnode<T> *w = NULL, llnode<T> *y = NULL) : key(k), prev(w), next(y){};
~llnode()
{
delete this;
}
};
But, when I run the program this code in the main
function但是,当我在
main
函数中运行此代码时
llnode<int> *node;
node->key = 6;
llnode<int> *node1;
node->key = 2;
I get the error message:我收到错误消息:
403 cygwin_exception::open_stackdumpfile: Dumping stack trace to "NAME OF MY EXE".exe.stackdump
How can I create more nodes without getting the error?如何在不出现错误的情况下创建更多节点? It happens when I have 2 nodes created, but when I have 1 it does it right.
当我创建了 2 个节点时会发生这种情况,但是当我创建了 1 个节点时,它就正确了。
First, for首先,对于
llnode<int> *node;
node->key = 6;
llnode<int> *node1;
node->key = 2;
your problem is that node
is an uninitialized pointer.你的问题是
node
是一个未初始化的指针。 You should always initialize your pointers (or better, use smart pointers like unique_ptr
).你应该总是初始化你的指针(或者更好的是,使用像
unique_ptr
这样的智能指针)。 Try:尝试:
std::unique_ptr<llnode<int>> node = std::make_unique<llnode<int>>();
node->key = 6;
std::unique_ptr<llnode<int>> node1 = std::make_unique<llnode<int>>();
node->key = 2;
or better:或更好:
auto node = std::make_unique<llnode<int>>(6);
auto node1 = std::make_unique<llnode<int>>(2);
In general, this is very C-like C++.一般来说,这是非常像 C 语言的 C++。 Raw pointers are hard to use correctly, particularly when it comes to exception safety.
原始指针很难正确使用,尤其是在涉及异常安全时。 For that reason, I make an effort to never write
new
or delete
ever.出于这个原因,我努力永远不要写
new
或delete
的。 (There are places, but really, you want to avoid them.) (有些地方,但实际上,您想避开它们。)
You can use std::unique_ptr
to do cleanup (and initialization) for you.您可以使用
std::unique_ptr
为您进行清理(和初始化)。 You can also default raw pointers to nullptr
.您还可以默认指向
nullptr
原始指针。
In C++11 and beyond, don't use NULL
, use nullptr
, it's safer.在 C++11 及更高版本中,不要使用
NULL
,使用nullptr
,它更安全。
Consider something like this:考虑这样的事情:
#include <memory>
template <typename T>
class llnode
{
public:
T key;
llnode<T>* prev = nullptr; // Raw pointer back
std::unique_ptr<llnode<T>> next; // List owns it tail.
llnode(const T &k = {},
llnode<T> *w = nullptr,
std::unique_ptr<llnode<T>> y = nullptr)
: key(k), prev(w), next(std::move(y)) {}
};
When this node is deleted, the node pointed to by next
(if next != nullptr
) will also be deleted for you.当这个节点被删除时,
next
指向的节点(如果next != nullptr
)也会为你删除。
Often a doubly-linked linked list will have a separate type that hides the nodes from the user, maintaining access to the front and back.通常一个双向链表会有一个单独的类型,它对用户隐藏节点,保持对前端和后端的访问。 Then you can provide standard operations such as
list<T>.push_back(const T&)
.然后您可以提供标准操作,例如
list<T>.push_back(const T&)
。
When you use them in main your pointers are not initializated.当您在 main 中使用它们时,您的指针不会被初始化。 This ends in Undefined Behavior.
这以未定义的行为结束。
Do this:做这个:
llnode<int> *node = new llnode<int>;
node->key = 6;
llnode<int> *node1 = new llnode<int>;
node->key = 2;
Also note that calling delete this;
还要注意调用
delete this;
in the destructor causes an infinite loop (More info here ).在析构函数中导致无限循环(更多信息在这里)。 Remove the destructor and free the space manually or better use a Smart Pointers or RAII approach.
手动移除析构函数并释放空间,或者更好地使用智能指针或 RAII 方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.