0

セグメンテーション違反が発生するのはなぜですか? gdb で実行したところ、でクラッシュしましたtail_playlist -> next = p_playlist;

struct playlist_ {
  int album;
  int track_num;
  struct playlist_ *next;
};
typedef struct playlist_  playlists;

struct users_ {
  int user_ID;
  struct playlist_ *playlist;
  struct users_ *next;
};
typedef struct users_ users;

int add_playlist(users *user_pointer,int user_ID,int album_ID,int track_num){

  playlists *head_playlist,*tail_playlist,*curr_playlist,*p_playlist;
  users *curr_users;

  curr_users = user_pointer;
  while(curr_users){

    /* Find this user in link list */
    if(curr_users -> user_ID == user_ID){
      curr_playlist = curr_users -> playlist;

      /* Check if playlist is empty for this user if so, update the empty list*/
      if(curr_playlist -> album == NULL && curr_playlist -> track_num == NULL){
        curr_playlist -> album = album_ID;
        curr_playlist -> track_num = track_num;
        curr_playlist -> next = NULL;
      }else{

        /*Creates a new playlist node with the request album and track number*/
        p_playlist = (playlists *)malloc(sizeof(playlists ));
        p_playlist -> album = album_ID;
        p_playlist -> track_num = track_num;
        p_playlist -> next = NULL;

        /*Traverse Playlist, find the tail node*/
        for(curr_playlist = curr_users -> playlist; curr_playlist -> next; curr_playlist = curr_playlist -> next){

          /*Adds the playlist at the end*/

          tail_playlist = curr_playlist;
        };

        tail_playlist -> next = p_playlist;
      }
      break;
    }
    curr_users = curr_users -> next;
  }
}
4

1 に答える 1

1

あなたが持っている

for (curr_playlist = curr_users -> playlist; curr_playlist -> next; curr_playlist = cure_playlist -> next){
    tail_playlist = curr_playlist;
}

が最初の反復である場合curr_playlist -> nextはどうなりますか?NULLその後tail_playlist、設定されることはありませんが、次の行で逆参照されます。

tail_playlist -> next = p_playlist;
于 2012-04-04T01:31:43.853 に答える