2016-11-04 3 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; 
} 

:이 기능은 아래에 내가 segfault의 원인이 뭐하는 거지

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

재생 목록을 찾는 부분과 노래를 추가하는 부분으로 나눠야합니다. 그렇게하면 어느 것이 잘못 됐는지를 쉽게 알 수있을뿐만 아니라 일반적으로 더 깨끗합니다. 또한'struct song'을 모든 필드 대신에 인수로 전달할 수 있습니다. – Ryan

+0

게시 됨 (주 진입 점 없음)으로 코드가 세그먼테이션 오류가 발생하지 않습니다 .... – jpo38

답변

4

segfault가 발생하는 위치를 정확하게 알아낼 수 있도록 충분한 정보를 게시하지 않았습니다. MCVE example에서 분리 해보십시오.

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; 
} 

당신은 아마 의미 : 두 번째에 playPoint == NULL가 루프, 당신이 playPoint->title에 액세스하여 어쨌든 그것을 사용하게로하면서 때

그러나, 독방 감금 오류가 확실히 발생할 수

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 결함이 발생할 수 있습니다. 프로젝트를 처음부터 다시 시작하고 반복으로 항목을 추가하거나 (각 반복 테스트 및 유효성 검사) .... 또는 디버거를 사용하여 모두 수정하십시오.

관련 문제