2016-11-04 11 views
-3

現在のCSクラスのリンクリストを含むプログラムを作成しています。機能は以下の通りです:SegfaultがC言語のリンクリストを持っています

void addSong(Playlist *theList, char *name, char *title, char *artist, int minutes, int seconds) { 
    /* 
     1. Make sure a playlist by that name exists (so you can add a song to it) 
     2. Make sure the song does not already exist in the playlist (title/artist) 
     3. Add the new song to the end of the songlist in that playlist (add-at-end) 
    */ 
    Playlist *Pointer = theList; 
    while(1){//Find the list 
     if(strcmp(Pointer->name, name) == 0) 
      break; 
     if(Pointer->next == NULL){ 
      printf("There is no playlist by that name.\n"); 
      return; 
     } 
     Pointer = Pointer->next; 
    } 
    Song *playPoint = Pointer->songlist; 
    while(1){//Find the end of the list 
     if(playPoint == NULL){ 
      Song *Songy = malloc(sizeof(Song)); 
      Songy->title = title; 
      Songy->artist = artist; 
      Songy->minutes = minutes; 
      Songy->seconds = seconds; 
      Pointer->songlist = Songy; 
     } 
     if(strcmp(playPoint->title, title) == 0 && strcmp(playPoint->artist, artist) == 0){ 
      printf("There is already a song by that title and artist."); 
      return; 
     } 
     if(playPoint->next == NULL){ 
      break; 
     } 
     playPoint = playPoint->next; 
    } 
    Song *Songy = malloc(sizeof(Song)); 
    Songy->title = title; 
    Songy->artist = artist; 
    Songy->minutes = minutes; 
    Songy->seconds = seconds; 
    playPoint->next = Songy; //Add the song to the end of the list 
    return; 
} 

それが重要ならば、ここで参照2つの構造体です:私は、セグメンテーションフォルトを引き起こすことが

typedef struct song { 
    char *title; 
    char *artist; 
    int minutes; 
    int seconds; 
    struct song *next; 
} Song; 

typedef struct playlist { 
    char *name; 
    Song *songlist; 
    struct playlist *next; 
} Playlist; 

何をしているのですか?

+0

デバッガーを試しましたか? –

+0

これを2つの部分に分割する必要があります.1つは再生リストを検索する部分と、もう1つは再生リストを追加する部分です。そうすれば、どちらが間違っているのかをより簡単に知ることができます。すべてのフィールドの代わりに 'struct song'を引数として渡すこともできます。 – Ryan

+0

投稿された(メインエントリポイントなし)ので、コードはセグメンテーションフォールトを行いません.... – jpo38

答えて

4

segfaultが発生する場所を正確に特定できるようにするために、十分な情報を投稿していませんでした。 MCVE exampleで隔離することを検討してください。あなたの第二中ときplayPoint == NULLあなたがplayPoint->titleにアクセスして、とにかくそれを使用して終了と

しかし、ワンセグ障害は、ループしながら、確実に発生する可能性があります:

if(playPoint == NULL){ 
    Song *Songy = malloc(sizeof(Song)); 
    Songy->title = title; 
    Songy->artist = artist; 
    Songy->minutes = minutes; 
    Songy->seconds = seconds; 
    Pointer->songlist = Songy; 
} 
// here, playPoint is still equal to NULL!! COde from your if statement did not change that! 
// accessing playPoint->title and playPoint->artist will crash for sure (seg fault) 
if(strcmp(playPoint->title, title) == 0 && strcmp(playPoint->artist, artist) == 0){ 
    printf("There is already a song by that title and artist."); 
    return; 
} 

おそらく意味:

if(playPoint == NULL){ 
     playPoint = malloc(sizeof(Song)); 
     playPoint->title = title; 
     playPoint->artist = artist; 
     playPoint->minutes = minutes; 
     playPoint->seconds = seconds; 
     Pointer->songlist = playPoint; 
} 

しかし、推測するのは難しいです...

しかし、このコードでは、Segfaultの他のソースがある可能性があります(Songy-> Ryanコメントしました)+他のコードでは投稿しませんでした。

テストを開始する前に多すぎるコードを書いたことがあります。状況が間違っていてsegフォルトが発生する可能性がある場所が多分あるでしょう。あなたのプロジェクトを最初からやり直して、反復で物事を追加する(各反復のテストと検証)....またはデバッガーを使ってすべてを修正することを検討してください...

関連する問題