2012-04-05 61 views
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; 
    } 
} 

回答

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塊都在分配和初始化播放列表結構的模塊性問題)。

+0

我剛剛添加了我如何創建用戶。我成功創建了一個用戶鏈接列表'users_pointer'指向它。我也把它傳遞給'add_playlist()'和'print_account'。我運行add_playlist(),然後print_account然後當我嘗試再次運行add_playlist時,我得到錯誤。 – 2012-04-06 00:10:07

+0

我剛看到您的更新,它擺脫了分段故障錯誤。非常感謝。 – 2012-04-06 00:13:56

+0

@LearningC:我查看了顯示用戶列表初始化的更新,我不確定我的答案是否正確。我猜測(猜測)該列表最初並不包含空的播放列表元素......但它確實如此。所以我仍然有點困惑。如果該更改消除了seg故障,則它將指示指針爲NULL(或未初始化)。但是這與顯示用戶元素初始化的新代碼不太一致。 – 2012-04-06 00:18:18