C++ 使用c+中的节点获取一个奇怪的值+;
我希望以前没有人问过这个问题。我试图寻找答案,但没有找到。我正在尝试学习C++中的指针和链表。这是到目前为止我的代码C++ 使用c+中的节点获取一个奇怪的值+;,c++,conditional-statements,C++,Conditional Statements,我希望以前没有人问过这个问题。我试图寻找答案,但没有找到。我正在尝试学习C++中的指针和链表。这是到目前为止我的代码 #include <iostream> using namespace std; struct node{ int value; node* next; }; node createNode(int value){ node temp; temp.value = value; temp. next = NULL; return temp; } vo
#include <iostream>
using namespace std;
struct node{
int value;
node* next;
};
node createNode(int value){
node temp;
temp.value = value;
temp. next = NULL;
return temp;
}
void add( node*& head, int value){
node temp = createNode(value);
// the problem is in this if statement
if (head->next == NULL){
head->next = &temp;
}
}
int main() {
node* head;
add(head,4);
cout<<head->next->value;
return 0;
}
#包括
使用名称空间std;
结构节点{
int值;
节点*下一步;
};
节点createNode(int值){
节点温度;
温度值=温度值;
温度next=NULL;
返回温度;
}
void add(节点*&头部,int值){
节点温度=createNode(值);
//问题在于这个if语句
if(head->next==NULL){
头部->下一步=&temp;
}
}
int main(){
节点*头;
增加(总目4);
库特价值;
返回0;
}
如果我试图将temp的地址抓取到head中,在我的主函数中,cout将打印这个值“1634454”,如果我将代码行“head->next=&temp;”移到if语句之外,它将打印正确的结果,即4。我不明白为什么会这样!这个值是多少?if语句中的条件对地址或head->next的值有影响吗?您将
temp
声明为节点(注意缺少*
),即局部变量。因此,它会在函数结束时自动释放
粗略地说,您需要决定如何管理内存。如果您想自己完成这一切,您需要在任何地方使用node*
,并记住在数据结构超出范围时释放它们。但更现代、更安全的方法是使用unique\u ptr
,它可以正确捕获节点拥有其next
指针。通过这种方式,数据将在时间到来时自动释放,而不是在时间到来之前。正如@Silvio Mayolo提到的,您有两种选择,可以使用自我管理内存或智能指针,但由于您的基本代码是以自我管理的方式编写的,因此我修复了代码中的一些指针。
名为head的节点指针未分配内存,temp变量most应为指针,以便生命周期剂量超过,如果使用c++11及更高版本,则使用nullptr
会更好
#include <iostream>
using namespace std;
struct node{
int value;
node* next=nullptr;
};
node* createNode(int value){
node* temp=new node;
temp->value = value;
temp->next = nullptr;
return temp;
}
void add( node* head, int value){
node* temp = createNode(value);
if (head->next == nullptr){
head->next = temp;
}
}
int main() {
node* head=new node;
add(head,4);
cout<<head->next->value;
return 0;
}
#包括
使用名称空间std;
结构节点{
int值;
node*next=nullptr;
};
节点*createNode(int值){
node*temp=新节点;
温度->值=值;
temp->next=nullptr;
返回温度;
}
void add(节点*头部,int值){
node*temp=createNode(值);
if(head->next==nullptr){
头部->下一步=温度;
}
}
int main(){
节点*头=新节点;
增加(总目4);
库特价值;
返回0;
}
#包括
使用名称空间std;
结构节点{
int值;
node*next=NULL;//node*next;
};
node*createNode(int值){//node createNode(int值){
node*temp=new node;//node temp;
温度->值=值;
temp->next=NULL;
返回温度;
}
void add(节点*头,int值){//void add(节点*头,int值){
node*temp=createNode(值);//node temp=createNode(值);
if(head->next==NULL){
head->next=temp;//head->next=&temp;
}
}
int main(){
node*head=新节点;//node*head;
增加(总目4);
库特价值;
返回0;
}
head->next=&temp;
指定一个局部变量的地址。离开函数后,该变量将被销毁,指针next
将悬空。如果要这样做,必须通过new
创建节点
实例。不过,使_唯一
或使_共享
可能是更好的选择。另外,head
是未初始化的,使用它会导致未定义的行为。我明白了,这现在是有意义的。我忽略了temp将在函数末尾被释放的事实。非常感谢!但是为什么我删除if语句并保留“head->next=&temp;”呢代码中的第行,它正常给出了结果?您还做了很多其他更改,没有任何解释。我已经修复了第一个问题并添加了一些解释
#include<iostream>
using namespace std;
struct node{
int value;
node* next=NULL; //node* next;
};
node* createNode(int value){ //node createNode(int value){
node* temp=new node; //node temp;
temp->value = value;
temp->next = NULL;
return temp;
}
void add( node* head, int value){ //void add( node*& head, int value){
node* temp = createNode(value); //node temp = createNode(value);
if (head->next == NULL){
head->next = temp; //head->next = &temp;
}
}
int main() {
node* head=new node; //node* head;
add(head,4);
cout<<head->next->value;
return 0;
}