简体   繁体   中英

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

I made some code to understand how linked lists work in C++ and before the program terminates it says the error "exited with non-zero status". I am currently using an online compiler repl.it to test C++ code, i am not sure if this problem is related. How do i fix it? Here is my code. details details details details details detailsdetails details detailsdetails details detailsdetails details detailsdetails details details

#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;
}

The output is

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;

At this time t is the 3rd node you create and at this time this node have no value for is next attribute.

You can use a debugger as gdb to see more easily this kind of problem ( but maybe in your online compiler you can't)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM