关于对象引用的混淆
Confusion regarding reference of object
我正在解决关于 hackerrank 的链表问题。问题很简单:在链表的头部插入一个节点。我只需要实现将节点插入链表头部的功能即可。链表 class 和其他必要的功能已经定义。这是已经给出的代码:
#include <bits/stdc++.h>
using namespace std;
class SinglyLinkedListNode {
public:
int data;
SinglyLinkedListNode * next;
SinglyLinkedListNode(int node_data) {
this - > data = node_data;
this - > next = nullptr;
}
};
class SinglyLinkedList {
public:
SinglyLinkedListNode * head;
SinglyLinkedListNode * tail;
SinglyLinkedList() {
this - > head = nullptr;
this - > tail = nullptr;
}
};
void print_singly_linked_list(SinglyLinkedListNode * node, string sep, ofstream & fout) {
while (node) {
fout << node - > data;
node = node - > next;
if (node) {
fout << sep;
}
}
}
void free_singly_linked_list(SinglyLinkedListNode * node) {
while (node) {
SinglyLinkedListNode * temp = node;
node = node - > next;
free(temp);
}
}
// Complete the insertNodeAtHead function below.
/*
* For your reference:
*
* SinglyLinkedListNode {
* int data;
* SinglyLinkedListNode* next;
* };
*
*/
SinglyLinkedListNode * insertNodeAtHead(SinglyLinkedListNode * head, int data) {
}
这个函数我有两个实现:
首先...
SinglyLinkedListNode * insertNodeAtHead(SinglyLinkedListNode * head, int data) {
SinglyLinkedListNode node(data);
node.next = head;
head = &node;
return head;
}
第二个....
SinglyLinkedListNode * insertNodeAtHead(SinglyLinkedListNode * head, int data) {
SinglyLinkedListNode * ptr = new SinglyLinkedListNode(data);
ptr - > next = head;
head = ptr;
return head;
}
第二个有效但第一个给出运行时间error.What发生了什么?
在第一个示例中,您将局部变量的地址分配给 head
。一旦函数 returns,此变量超出范围,并且 head
指向无效对象。
第一个用自动storage duration创建了一个SinglyLinkedListNode
,意思是对象一旦insertNodeAtHead
returns就失效了。实际上,该对象很可能是在堆栈上分配的。
第二个使用动态存储持续时间,这意味着它一直有效,直到您使用 delete
取消分配它。
我正在解决关于 hackerrank 的链表问题。问题很简单:在链表的头部插入一个节点。我只需要实现将节点插入链表头部的功能即可。链表 class 和其他必要的功能已经定义。这是已经给出的代码:
#include <bits/stdc++.h>
using namespace std;
class SinglyLinkedListNode {
public:
int data;
SinglyLinkedListNode * next;
SinglyLinkedListNode(int node_data) {
this - > data = node_data;
this - > next = nullptr;
}
};
class SinglyLinkedList {
public:
SinglyLinkedListNode * head;
SinglyLinkedListNode * tail;
SinglyLinkedList() {
this - > head = nullptr;
this - > tail = nullptr;
}
};
void print_singly_linked_list(SinglyLinkedListNode * node, string sep, ofstream & fout) {
while (node) {
fout << node - > data;
node = node - > next;
if (node) {
fout << sep;
}
}
}
void free_singly_linked_list(SinglyLinkedListNode * node) {
while (node) {
SinglyLinkedListNode * temp = node;
node = node - > next;
free(temp);
}
}
// Complete the insertNodeAtHead function below.
/*
* For your reference:
*
* SinglyLinkedListNode {
* int data;
* SinglyLinkedListNode* next;
* };
*
*/
SinglyLinkedListNode * insertNodeAtHead(SinglyLinkedListNode * head, int data) {
}
这个函数我有两个实现: 首先...
SinglyLinkedListNode * insertNodeAtHead(SinglyLinkedListNode * head, int data) {
SinglyLinkedListNode node(data);
node.next = head;
head = &node;
return head;
}
第二个....
SinglyLinkedListNode * insertNodeAtHead(SinglyLinkedListNode * head, int data) {
SinglyLinkedListNode * ptr = new SinglyLinkedListNode(data);
ptr - > next = head;
head = ptr;
return head;
}
第二个有效但第一个给出运行时间error.What发生了什么?
在第一个示例中,您将局部变量的地址分配给 head
。一旦函数 returns,此变量超出范围,并且 head
指向无效对象。
第一个用自动storage duration创建了一个SinglyLinkedListNode
,意思是对象一旦insertNodeAtHead
returns就失效了。实际上,该对象很可能是在堆栈上分配的。
第二个使用动态存储持续时间,这意味着它一直有效,直到您使用 delete
取消分配它。