c - 在段错误中获取和从链接列表中删除结果

标签 c segmentation-fault

从链接列表中检索消息,然后从列表中删除该消息会导致段错误。

第一条消息和服务器消息结构:

// used to store messages.
struct server_message {
    char message[80];
    char id[80];
    struct server_message *next_msg;
};

//head of the list
static struct server_message *first_message = NULL;

获取消息功能

char *get_message(char *id) {
    char *message;
    char *not_found =(char *)  malloc( sizeof(char) * 20 );
    not_found = "No messages available";
    struct server_message *curr_msg = first_message;
    struct server_message *prev_msg = first_message;

    if (curr_msg != NULL && (strcmp(curr_msg->id, id) != 0)) {
        strcpy (message, curr_msg->message);
        //Remove message
        first_message = NULL;
        return message;
    }


    while (curr_msg->next_msg != NULL) {
        curr_msg = curr_msg->next_msg;

        if (strcmp(curr_msg->id, id) != 0) {
            strcpy (message, curr_msg->message);
            //Remove message
            prev_msg->next_msg = curr_msg->next_msg;
            return message;
        } else {
            prev_msg = curr_msg;
        }
    }
    return not_found;
}

更新后的代码仍然出现段错误

char *get_message(char *id) {
    char *message = (char *)  malloc( sizeof(char) * 80 );
    char *not_found=(char *)  malloc( sizeof(char) * 20 );
    strcpy(not_found, "No messages available");
    struct server_message *curr_msg = first_message;
    struct server_message *prev_msg = NULL;

    while (curr_msg->next_msg != NULL) {

        if (strcmp(curr_msg->id, id) != 0) {
            strcpy (message, curr_msg->message);
            //Remove message
            if (prev_msg == NULL) {
                first_message = curr_msg->next_msg;
            } else {
                prev_msg->next_msg = curr_msg->next_msg;
            }
            return message;
        } else {
            prev_msg = curr_msg;
            curr_msg = curr_msg->next_msg;
        }
    }
    return not_found;
}

最佳答案

修改后的完整示例:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// used to store messages.
struct server_message {
    char message[80];
    char id[80];
    struct server_message *next_msg;
};

//head of the list
static struct server_message *first_message = NULL;

char *get_message(char *id) {
    struct server_message *curr_msg = first_message;
    struct server_message *prev_msg = NULL;

    while (curr_msg != NULL) {               /* FIX1: iterate until we do not have an item */
        if (strcmp(curr_msg->id, id) == 0) { /* FIX2: 0 is equal */
            //Remove message
            if (prev_msg == NULL) {
                first_message = curr_msg->next_msg;
            } else {
                prev_msg->next_msg = curr_msg->next_msg;
            }
            return strdup(curr_msg->message);
        } else {
            prev_msg = curr_msg;
            curr_msg = curr_msg->next_msg;
        }
    }
    return strdup ("No messages available");
}

struct server_message d = {"foo-4", "4", 0};
struct server_message c = {"foo-3", "3", &d};
struct server_message b = {"foo-2", "2", &c};
struct server_message a = {"foo-1", "1", &b};

int main(int argc, char *argv[])
{
        char *t;

        first_message = &a;

        t = get_message("1");
        printf ("1: %s\n", t);
        free (t);

        t = get_message("3");
        printf ("3: %s\n", t);
        free (t);

        t = get_message("10");
        printf ("10: %s\n", t);
        free (t);

        return 0;
}

关于c - 在段错误中获取和从链接列表中删除结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36911521/

相关文章:

计算给定边界框以及图像的宽度和高度的比例

c++ - 如何使用 fgets 获取整行

c - strncat() 上的段错误

c++ - C++类网络编程的段错误

c - 为什么在指针赋值后会出现段错误?

c - 释放无效指针?

C 运算符优先级,后增量编程问题

c - 如何使用 IP 的整数值(在 C 中)确定两个 IP 是否位于同一子网中?

c - 我的自定义操作系统内核中的 VGA 显示问题

c - codechef 中的运行时错误 (SIGSEGV)