ListNode的初始化

发布于 2025-01-17 08:16:41 字数 442 浏览 4 评论 0原文

我有一个关于 ListNode 初始化的问题,如果我只是宣布一个 ListNode 指针,为什么我不能像代码中所示分配它的下一个值。

struct ListNode {
     int val;
     ListNode *next;
     ListNode(int x) : val(x), next(nullptr) {}
};

ListNode* tmp;
tmp->next = nullptr;// This is wrong, why is that?

ListNode* tmp2 = new ListNode(1);
tmp2->next = nullptr;// This is right, what cause that?

我只是尝试宣布一个 ListNode 指针并将其下一个值分配给 nullptr。但是当我宣布一个带有新函数的指针后,它就正常了。为什么?

I have a question about the initialization of ListNode, if I just announce a ListNode pointer, why can't I assign its next value like showed in the code.

struct ListNode {
     int val;
     ListNode *next;
     ListNode(int x) : val(x), next(nullptr) {}
};

ListNode* tmp;
tmp->next = nullptr;// This is wrong, why is that?

ListNode* tmp2 = new ListNode(1);
tmp2->next = nullptr;// This is right, what cause that?

I just try to announce a ListNode pointer and assign its next value to nullptr. But after I announce a pointer with new function, it goes right. Why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静水深流 2025-01-24 08:16:41

指针是一个可以保存对象地址的盒子

 ListNode* tmp;
 tmp->next = nullptr;// This is wrong, why is that?

在这里您创建了盒子(tmp),但没有将对象的地址放入其中。
第二行说 - “在距 tmp 中存储的地址偏移 4 处,请写入 0”,框中没有有效地址,因此失败。

在第二个例子中,

ListNode* tmp2 = new ListNode(1);
tmp2->next = nullptr;// This is right, what cause that?

你说

"Please make a new ListNode object"
"Put its address in the box called tmp2"
"at offset 4 from the address stored in tmp2 please write 0"

这可以工作,因为框 tmp2 指向有效的地方

A pointer is a box that can hold the address of an object

 ListNode* tmp;
 tmp->next = nullptr;// This is wrong, why is that?

Here you created the box (tmp) but did not put the address of an object in it.
The second line says - "at offset 4 from the address stored in tmp please write 0", well there is no valid address in the box so this fails.

In the second example

ListNode* tmp2 = new ListNode(1);
tmp2->next = nullptr;// This is right, what cause that?

You say

"Please make a new ListNode object"
"Put its address in the box called tmp2"
"at offset 4 from the address stored in tmp2 please write 0"

That can work, becuase the box tmp2 points somewhere valid

め七分饶幸 2025-01-24 08:16:41

我只是尝试宣布一个 ListNode 指针并将其下一个值分配给
空指针

该指针没有任何数据成员。它是一个具有数据成员的 ListNode 类型的对象。

因此,您需要一个 ListNode 类型的对象,并使用指向该对象的指针,您可以为该对象的数据成员 next 分配任何值。

ListNode* tmp2 = new ListNode(1);
^^^^^^^^^^^^^^       ^^^^^^^^^^^
  pointer               object

对于此代码片段

ListNode* tmp;
tmp->next = nullptr;// This is wrong, why is that?

Listnode 类型的对象都不存在您可以更改的数据成员。此外,由于指针 tmp 未初始化并且具有不确定的值,因此使用此类指针访问内存会导致未定义的行为。

I just try to announce a ListNode pointer and assign its next value to
nullptr

The pointer does not have any data members. It is an object of the type ListNode that has data members.

So you need an object of the type ListNode and using a pointer pointing to the object you can assign any value to the data member next of the object.

ListNode* tmp2 = new ListNode(1);
^^^^^^^^^^^^^^       ^^^^^^^^^^^
  pointer               object

As for this code snippet

ListNode* tmp;
tmp->next = nullptr;// This is wrong, why is that?

then neither object of the type Listnode exists data members of which you could change. Moreover as the pointer tmp is uninitialized and has an indeterminate value then using such a pointer to access memory results in undefined behavior.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文