2017-07-04 6 views
-3
#include<iostream> 
#include<cstdio> 
#include<cstdlib> 

using namespace std; 

struct Node { 
    int data; 
    Node* next; 
}; 

struct Node* takeInput(){ 
    struct Node* head; 
    cout<<"Enter element:"; 
    int data; 
    cin>>data; 
    while(data!=-1){ 
     struct Node* newNode=new (struct Node); 
     newNode->data=data; 
     newNode->next=NULL; 

     if(head==NULL){ 
      head=newNode; 
     } 
     else{ 
      struct Node* temp=new (struct Node); 
      while(temp->next!=NULL){ 
       temp=temp->next; 
      } 
      temp->next=newNode; 
     } 
     cout<<"Enter next element: "; 
     cin>>data; 
    } 
    return head; 
} 

void print(struct Node* head){ 


    while(head->next!=NULL){ 
     cout<<head->data<<"->"; 
     head=head->next; 
    } 
} 

int main(){ 
    struct Node* head = new (struct Node); 
    head = takeInput(); 
    print(head); 
} 

セグメント化エラーは、print()関数が実行されるときに発生します。 印刷機能を実行しないと、コードは完全に実行されます。コードはユーザーからの入力を受け取りますが、リンクされたリストを印刷しようとするとクラッシュします。 Linux OSでCode Blocks IDEでgccコンパイラを使用しています。このコードを実行すると、私のコンパイラは私にセグメンテーションフォルトを与えますか?

+7

コンパイラは、あなたが障害をセグメンテーション与えるものではありません。それはそれを "与える"あなたのプログラムです。 –

+1

あなたが 'print'関数に' nullptr'を渡すとどうなりますか? – iehrlich

+0

'while(temp-> next!= NULL){'はメンバーが初期化されていない新しいノードの 'next'をチェックしています。新しいノードを割り当てるのではなく、直前に 'temp = head;'にすることを意味します。 –

答えて

1

あなたのコードは初期化されていないメンバーを間接参照し、初期化されていない変数へのアクセスに満ちている、とメモリリークを生成します

if(head==NULL)headは、初期化子なしのローカル変数

tempがちょうど作成されたと next

while(head->next!=NULL)が割り当てられたことがない、headはおそらくNULL

struct Node* head = new (struct Node); head = takeInput()が漏れている関数の引数であり、ここで

while(temp->next!=NULL)、。

struct Node* temp=new (struct Node); ... temp=temp->nextリーク。ずっとにあなたのコードを変更せずに、次のように動作するはず

struct Node* takeInput(struct Node* head) { 
    cout<<"Enter element:"; 
    int data; 
    cin>>data; 
    while(data!=-1){ 
     struct Node* newNode=new (struct Node); 
     newNode->data=data; 
     newNode->next=NULL; 

     if(head==NULL){ 
      head=newNode; 
     } 
     else{ 
      struct Node* temp=head; // start at the head 
      while(temp->next!=NULL){ 
       temp=temp->next; 
      } 
      temp->next=newNode; 
     } 
     cout<<"Enter next element: "; 
     cin>>data; 
    } 
    return head; 
} 

void print(struct Node* head){ 


    while(head!=NULL){ // test head, not it's successor 
     cout<<head->data<<"->"; 
     head=head->next; 
    } 
} 

int main(){ 
    struct Node* head = takeInput(NULL); 

    print(head); 
} 
0

struct Node* head;は、if(head==NULL)をチェックするとほとんど初期化されませんが、ほとんど間違いと評価されます。 elseで行うことは、リークメモリだけで、初期化されていないポインタを返すことです。あなたがそれを使用しようとすると、それはちょうどセグフルトになります。

struct Node* head = takeInput();への主な変更struct Node* head = new (struct Node);

struct Node* temp=head;から struct Node* head=NULL;struct Node* temp=new (struct Node);に変更 struct Node* head;とメモリを解放することを忘れないでください

+0

完璧に動作します..ありがとうございました! –

+1

"' if(head == NULL) 'これはほぼ確実にfalseと評価されます - 本当ではありません。それは未定義の振る舞いであり、クラッシュにつながる可能性があり、完全に最適化されている可能性があります。未定義の振る舞いがあれば、評価の結果について何も仮定しないかもしれないことに注意する価値があると思います... –

関連する問題