c - 在 C 中调试段错误

标签 c

我正在尝试制作一个排序的链表。一切看起来都很好,但由于某种原因我遇到了段错误,不知道为什么。任何帮助,将不胜感激。问题出在我的 while 循环中,我在下面的代码中对此进行了评论。

typedef struct node{
    char *name;
    struct node *next;
} node;

node* insert_node(node *head, char *name){
    node *temp, *pre, *next;
    temp = (node*) malloc(sizeof(node));
    temp->name = name;
    temp->next = NULL;

    if (!head){
        head = temp;
    } else {
        pre = NULL;
        next = head;
        //something is wrong with this while loop, not sure what though
        while(next && (strcmp(next->name, name) < 0) ){
            pre = next;
            next = next->next;
        }
        printf("out\n");
        if (!next){
            pre->next = temp;
        } else {
            if(pre){
                temp->next = pre->next;
                pre->next = temp;
            } else {
                temp->next = head;
                head = temp;
            }
        }
    }
    return head;
}

@qwn,要求我为 next 节点指针和临时节点取不同的名称。 @Jonathan Leffler,让我提供完整的代码。所以,在这里,

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

typedef struct node{
    char *name;
    struct node *next;
} node;

node* insert_node(node *head, char *name);
void print_links(node *head);
void free_links(node *head);
void parser(char *string);

int main (int argc, char *argv[]){
    char *string = "file1a.txt/file1b.txt/too_deep/dir1/file2a.txt/";
    parser(string);
}

void parser(char *s){
    node *head;
    char *tokens;
    char *string = strdup(s);
    tokens = strtok(string, "/");
    while (tokens != NULL){
        //printf("%s\n", tokens);
        head = insert_node(head, tokens);
        tokens = strtok(NULL, "/");
    }
    print_links(head);
    free_links(head);
    return;
}

node* insert_node(node *head, char *name){
    node *temp, *pre, *ptr;
    temp = (node*) malloc(sizeof(node));
    temp->name = name;
    temp->next = NULL;

    if (!head){
        head = temp;
    } else {
        pre = NULL;
        ptr = head;
        // problem lies in this while loop
        while(ptr && (strcmp(ptr->name, name) < 0) ){
            printf("here\n");
            pre = ptr;
            ptr = ptr->next;
        }
        printf("out\n");
        if (!ptr){
            pre->next = temp;
        } else {
            if(pre){
                temp->next = pre->next;
                pre->next = temp;
            } else {
                temp->next = head;
                head = temp;
            }
        }
    }
    return head;
}

最佳答案

你的head变量没有在parser()中初始化,它的undefined值被传递给了insert_node(),所以第一次访问使用 if(!head) 触发未定义行为的值。

结果很可能是“假”(如您的情况),因此控制被传递到 else 分支,其中 head 值被复制到ptr 变量。因此 ptrwhile 循环的条件中不是 NULL,因此 ptr->namestrcmp 调用之前被访问– 这次你很幸运,UB 导致 ptr“指向”的位置发生内存访问错误,从而导致崩溃。

解决方法:
通过初始化器扩展声明:

    node* head = NULL;

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

相关文章:

c - LD_PRELOAD 不能拦截系统调用,只能拦截libcalls?

c - 多客户端 UDP 聊天

c - 指向结构的指针数组

C: Segmentation Fault -- 堆栈实现链表

c++ - FindFirstFile 和 FindNextFile 问题

c - kernel.h中min宏中 "(void) (&_min1 == &_min2)"的作用是什么?

c - .dat 或 .txt 文件末尾的换行符?

c - C中的中断系统调用

c++ - X11/Xlib c/c++ 字符到 XKeyEvent 键码

c - typeof的返回类型是什么?