c - C 中链表的段错误

标签 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; 

我做了什么导致了段错误?

最佳答案

您没有发布足够的信息,无法让其他人准确地发现您的段错误发生的位置。考虑将其隔离在 MCVE example 中.

但是,当第二个 while 循环中的 playPoint == NULL 时,肯定会发生段错误,因为无论如何您最终都会通过访问 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->next 未设置,如 Ryan 评论)+在您未发布的其他代码中。

在开始测试之前,您可能编写了太多代码,并且可能在很多地方做错了事情并可能导致段错误。考虑从头开始重新开始您的项目,并通过迭代添加内容(测试和验证每次迭代)...或者使用调试器来修复所有这些...

关于c - C 中链表的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40416036/

相关文章:

C++结构数组初始化

将字符串从 main 复制到结构 c

python - 反向打印链表

c - 在二叉搜索树中插入节点 (C)

c - 从数组中访问变量全局结构

c - 自定义释放函数是否应该考虑与通用容器的兼容性?

c# - C# 结构的属性在 COM 和 VB6 中获得不可用的名称?

java - 电子表格数据 - 链表或 HashMap ?

c - 单链表C,打印

c - C UNIX 中的分支和管道