1

我被卡住了,无法弄清楚为什么会出现分段错误。当我将第一个添加添加到播放列表时,一切都很好。然后,当我打印播放列表然后尝试向播放列表添加更多内容时,我会遇到分段错误。gdb 在这里报告错误if( (curr_playlist -> album) == NULL && (curr_playlist -> track_num) == NULL){。谁能帮我找到问题?打印功能是否在修改链接列表?

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;

/*This is how I created the user list*/

fscanf(transaction_file,"%d\n",&account_number);

      /*Checks for empty list, if true creates the first user*/
      if( head_users == NULL){
        p_users = malloc(sizeof(users ));
        p_users -> user_ID = account_number;
        head_users = p_users;
        curr_users = p_users;
        head_users -> next = NULL;
        users_pointer = head_users;

      /*If list is not empty create new user and puts it in front of list*/
      }else{
        p_users = malloc(sizeof(users));
        p_users -> user_ID = account_number;
        curr_users = p_users;
        curr_users -> next = head_users;
        head_users = curr_users;
        users_pointer = head_users;
      }

      /*Create an empty playlist for user and set everything to null, empty playlist*/
        p_playlists = malloc(sizeof(playlists));
        curr_playlists = p_playlists;
        curr_playlists -> album = FALSE;
        curr_playlists -> track_num = FALSE;
        curr_playlists -> next = NULL;
        curr_users -> playlist = p_playlists;

int add_playlist(users *user_pointer,album *all_albums,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){
        tail_playlist = curr_users -> playlist;
        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*/
        curr_playlist = curr_users -> playlist;
        while(curr_playlist ){
          tail_playlist = curr_playlist;
          curr_playlist = curr_playlist -> next;
        }

        /* Adds the new playlist at the end */
          tail_playlist -> next = p_playlist;
      }

    }
    curr_users = curr_users -> next;
  }
  /*Updates the playlist hits for each new playlist node*/
  *all_albums[album_ID].playlist_hits[track_num] = *all_albums[album_ID].playlist_hits[track_num] + 1;
}

int print_account(album *all_albums,int account_number,users *user_pointer){
  users *curr_users;
  playlists *curr_playlist;

  int i;
  curr_users = user_pointer;

  /*Check playlist for each user if NULL, playlist is empty*/
  while (curr_users){
    if(curr_users -> user_ID == account_number){

      printf("ACCOUNT %d\n",curr_users->user_ID);
      if(curr_users -> playlist -> album == -100 && curr_users -> playlist -> track_num == -100){
        printf("  EMPTY Playlist\n");

      }else{
        /*Goes through the user playlist and prints out the information*/
        for(curr_playlist = curr_users -> playlist; curr_playlist; curr_playlist = curr_playlist -> next){
          printf("  ALBUM %d:  %s\n",curr_users -> playlist -> album,
                 all_albums[curr_users -> playlist -> album].tracks[curr_users -> playlist -> track_num]);
          curr_users -> playlist = curr_users -> playlist -> next;
        }
      }
    }
      curr_users = curr_users -> next;
  }
}
4

1 回答 1

2

问题的一部分似乎可能是假设curr_users -> playlist已经用指向“空”播放列表的指针进行了初始化。如果为NULL或者有一个未初始化的值,那么album在后面的语句中检查时会导致segmentation fault。该代码没有显示最初如何创建用户列表,所以我推测了一下。

如果用户列表最初不是使用“空”播放列表条目创建的,playlist则应将成员初始化为 NULL,并且此语句:

     if ( (curr_playlist -> album) == NULL && (curr_playlist -> track_num) == NULL )

应该可能是:

if ( curr_playlist == NULL )

然后它应该分配(通过malloc)一个类似于 else 部分的新播放列表。(当然,还有if块和else块都在分配和初始化播放列表结构的模块化问题)。

于 2012-04-06T00:00:55.147 回答