退出非零状态(RepL.it)C++? 我做了一些代码来理解链表如何在C++中工作,程序终止之前说它是用非零状态退出的。我目前使用的是一个在线编译器回复。它测试C++代码,我不确定这个问题是否相关。我怎么修理它?这是我的密码。详细信息详细信息详细信息详细信息详细信息详细信息详细信息详细信息详细信息详细信息详细信息 #include <iostream> #include <string> using namespace std; struct node{ int data; node* next; }; int main() { node* n; //new node* t; //temp node* h; //header n=new node; n->data=1; t=n; h=n; cout <<"Pass 1"<<endl; cout <<"t=" << t << endl; cout <<"n=" << t << endl; cout <<"h=" << h << endl; cout << n->data << endl; n=new node; n->data=2; t->next=n; t=t->next; cout <<"Pass 2"<<endl; cout <<"t=" << t << endl; cout <<"n=" << t << endl; cout <<"h=" << h << endl; cout << n->data << endl; n=new node; n->data=3; t->next=n; t=t->next; cout <<"Pass 3"<<endl; cout <<"t=" << t << endl; cout <<"n=" << t << endl; cout <<"h=" << h << endl; cout << n->data << endl; //Test pass //exits with non-zero status //NULL to pointer means invalid address; termination of program? n=new node; t=t->next; n->data=4; t->next=n; n->next=NULL; cout <<"Pass 4"<<endl; cout <<"t=" << t << endl; cout <<"n=" << t << endl; cout <<"h=" << h << endl; string a; a="End test"; cout << a << endl; return 0; }

退出非零状态(RepL.it)C++? 我做了一些代码来理解链表如何在C++中工作,程序终止之前说它是用非零状态退出的。我目前使用的是一个在线编译器回复。它测试C++代码,我不确定这个问题是否相关。我怎么修理它?这是我的密码。详细信息详细信息详细信息详细信息详细信息详细信息详细信息详细信息详细信息详细信息详细信息 #include <iostream> #include <string> using namespace std; struct node{ int data; node* next; }; int main() { node* n; //new node* t; //temp node* h; //header n=new node; n->data=1; t=n; h=n; cout <<"Pass 1"<<endl; cout <<"t=" << t << endl; cout <<"n=" << t << endl; cout <<"h=" << h << endl; cout << n->data << endl; n=new node; n->data=2; t->next=n; t=t->next; cout <<"Pass 2"<<endl; cout <<"t=" << t << endl; cout <<"n=" << t << endl; cout <<"h=" << h << endl; cout << n->data << endl; n=new node; n->data=3; t->next=n; t=t->next; cout <<"Pass 3"<<endl; cout <<"t=" << t << endl; cout <<"n=" << t << endl; cout <<"h=" << h << endl; cout << n->data << endl; //Test pass //exits with non-zero status //NULL to pointer means invalid address; termination of program? n=new node; t=t->next; n->data=4; t->next=n; n->next=NULL; cout <<"Pass 4"<<endl; cout <<"t=" << t << endl; cout <<"n=" << t << endl; cout <<"h=" << h << endl; string a; a="End test"; cout << a << endl; return 0; },c++,status,C++,Status,此时,t是您创建的第三个节点,此时此节点没有is next属性的值 您可以将调试器用作gdb,以便更容易地查看此类问题,但在联机编译器中,您可能无法检查在第4过程中执行操作的顺序。在那里取消引用未初始化的指针。以后请先使用调试器来发现这些问题。不管你有什么问题,不要随意使用new。每次使用new时,都会动态分配总是要删除的内存。要么使用unique_ptr,这是一个包装类,当节点`超出范围时,它会自动删除该节点,要么给节点类一个add_next方法,该方法在内部使用new并使~node do d

此时,t是您创建的第三个节点,此时此节点没有is next属性的值


您可以将调试器用作gdb,以便更容易地查看此类问题,但在联机编译器中,您可能无法检查在第4过程中执行操作的顺序。在那里取消引用未初始化的指针。以后请先使用调试器来发现这些问题。不管你有什么问题,不要随意使用new。每次使用new时,都会动态分配总是要删除的内存。要么使用unique_ptr,这是一个包装类,当节点`超出范围时,它会自动删除该节点,要么给节点类一个add_next方法,该方法在内部使用new并使~node do delete next。在后一种情况下,您必须小心地编写异常安全代码,这就是为什么您应该选择唯一的\u ptr解决方案。您始终在n=行上打印t。复制和粘贴太多?不幸的是,它是一个在线编译器,因此没有调试器。下次我将使用包装器类,谢谢。是的,它应该是n,但在这种情况下它恰好也等于t。
Pass 1
t=0x12efc20
n=0x12efc20
h=0x12efc20
1
Pass 2
t=0x12f0050
n=0x12f0050
h=0x12efc20
2
Pass 3
t=0x12f0070
n=0x12f0070
h=0x12efc20
3
exited with non-zero status
  n=new node;
  t=t->next;  <- error there
  n->data=4;
  t->next=n;
  n->next=NULL;