带有链接列表的Segfault

时间:2016-11-04 05:44:47

标签: c struct linked-list segmentation-fault

我正在为我当前的CS类编写一个涉及链表的程序,当我调用它时,一个函数特别会导致分段错误。功能如下:

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

如果重要,这里引用了两个结构:

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; 

我在做什么导致段错误?

1 个答案:

答案 0 :(得分:4)

您没有发布足够的信息,以便有人能够确切地发现您的段错误发生的位置。考虑将其隔离在MCVE example

然而,在第二个while循环中playPoint == NULL时肯定会发生seg错误,因为您最终通过访问playPoint->title来使用它:

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

你可能意味着:

if(playPoint == NULL){
        playPoint = malloc(sizeof(Song));
        playPoint->title = title;
        playPoint->artist = artist;
        playPoint->minutes = minutes;
        playPoint->seconds = seconds;
        Pointer->songlist = playPoint;
}

但很难猜到......

但是在这段代码中可能还有其他的段错误来源(比如Songy->接下来没有设置,就像Ryan评论的那样)+在其他代码中你没有发布。

在开始测试之前,您可能编写了太多代码,而且当事情出错并且可能导致seg错误时,您可能会有很多地方。考虑从头开始重新启动项目并通过迭代添加内容(测试和验证每次迭代)....或者使用debgger来修复它们......