以非零状态 (repl.it) C++ 退出?

Exited with non-zero status (repl.it) C++?

我编写了一些代码来了解链表在 C++ 中的工作方式,但在程序终止之前它会提示错误 "exited with non-zero status"。我目前正在使用在线编译器 repl.it 来测试 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;
}

输出为

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;

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

您可以使用调试器作为 gdb 来更容易地看到此类问题(但也许在您的在线编译器中您不能)