无法弄清楚这种分段错误的原因

时间:2012-04-05 23:50:19

标签: c pointers linked-list segmentation-fault structure

我陷入困境,无法弄清楚为什么会出现分段错误。当我将第一个添加添加到播放列表时,一切都很好。然后当我打印播放列表然后尝试添加更多的播放列表我得到分段错误。 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;
  }
}

1 个答案:

答案 0 :(得分:2)

似乎问题的一部分可能是curr_users -> playlist已使用指向“空”播放列表的指针初始化的假设。如果它为NULL或具有未初始化的值,则在后续语句中检查album时将导致分段错误。代码没有显示最初如何创建用户列表,所以我猜测了一下。

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

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

应该可能是:

if ( curr_playlist == NULL )

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