结构中char*上malloc的分段错误

结构中char*上malloc的分段错误,c,linked-list,segmentation-fault,malloc,C,Linked List,Segmentation Fault,Malloc,我在自学C语言,为了好玩,我想写一个小的web框架会很酷。我目前正在编写的代码是用于注册路由/处理程序的 我在malloc中遇到了一个分段错误,但奇怪的是,只有在第四次调用它时才出现。我已经在代码中标记了出现分段错误的点。除了用printfs和puts包装代码区域外,我不知道如何调试它。我曾尝试使用valgrind来调试它,但在运行valgrind时,实际上并没有出现分段错误。Valgrind确实告诉我,有一个内存泄漏源于相同的malloc。很明显我做错了什么,但我不知道是什么 仅供参考,des

我在自学C语言,为了好玩,我想写一个小的web框架会很酷。我目前正在编写的代码是用于注册路由/处理程序的

我在
malloc
中遇到了一个分段错误,但奇怪的是,只有在第四次调用它时才出现。我已经在代码中标记了出现分段错误的点。除了用
printf
s和
put
s包装代码区域外,我不知道如何调试它。我曾尝试使用valgrind来调试它,但在运行valgrind时,实际上并没有出现分段错误。Valgrind确实告诉我,有一个内存泄漏源于相同的
malloc
。很明显我做错了什么,但我不知道是什么

仅供参考,
destroy\u routes
在子进程终止之前调用

struct route {
    char *path;
    enum method method;
    int(*handler)(struct request *, int sock);
    struct route *next;
};


static struct route *routes;

void
register_route(char *path, enum method method,
               int(*handler)(struct request *, int))
{
    struct route *route;

    if (routes == NULL)
        routes = route = malloc(sizeof(struct route));
    else {
        route = routes;
        while (route->next)
            route = route->next;

        route->next = malloc(sizeof(struct route));
        route = route->next;
    }

    route->path = malloc((strlen(path) + 1) * sizeof(char)); /* <== HERE is where the segmentation fault occurs only on the fourth time */
    strcpy(route->path, path);

    route->method = method;
    route->handler = handler;

    printf("route created: %s, %i, %p\n", route->path, route->method, (void *)route->handler);
}

void
destroy_routes()
{
    struct route *prev;
    int i = 0;
    while (routes) {
        free(routes->path);
        prev = routes;
        routes = routes->next;
        free(prev);
        i++;
    }
    printf("destroyed %i routes\n", i);
}

void
register_routes()
{
    register_route("/post", GET, &do_something);
    register_route("/post", POST, &do_something_else);
    register_route("/post/:id", GET, &do_something);
    register_route("/post/:id/children", POST, &do_something_else);
}
struct路由{
字符*路径;
枚举法;
int(*handler)(结构请求*,int sock);
结构路由*下一步;
};
静态结构路由*路由;
无效的
寄存器路径(字符*路径,枚举方法,
int(*handler)(结构请求*,int))
{
结构路由*路由;
如果(路由==NULL)
routes=route=malloc(sizeof(struct-route));
否则{
路线=路线;
while(路线->下一步)
路由=路由->下一步;
route->next=malloc(sizeof(struct-route));
路由=路由->下一步;
}
route->path=malloc((strlen(path)+1)*sizeof(char));/*path,path);
路线->方法=方法;
路径->处理程序=处理程序;
printf(“创建的路由:%s,%i,%p\n”,路由->路径,路由->方法,(void*)路由->处理程序);
}
无效的
摧毁()
{
结构路由*prev;
int i=0;
while(路线){
自由(路线->路径);
prev=路线;
路由=路由->下一步;
免费(上);
i++;
}
printf(“销毁的%i个路由\n”,i);
}
无效的
注册路由()
{
注册路线(“/post”,GET,do\u something”);
注册路线(“/post”、post和do\u其他事项);
注册路径(“/post/:id”,GET,do\u something”);
注册路线(“/post/:id/children”,post,以及做其他事情);
}

您没有将
下一个
指针设置为空。这可能就是问题所在

route->method = method;
route->handler = handler;
route->next = NULL;

您没有将
next
指针设置为NULL。这可能就是问题所在

route->method = method;
route->handler = handler;
route->next = NULL;

routes=route=malloc(sizeof(struct-route)):首次设置的路由。routes->next未初始化。@BLUEPIXY第二次在
else
中,或者我盯着它看的时间太长了?@tjb1982
malloc
不会将内存归零,因此需要将链接列表中最后一个节点的
next
设置为空。您在任何地方都没有这样做。您需要通过
malloc
routes=route=malloc(sizeof(struct-route)),在创建
struct-route
时初始化所需的成员:首次设置的路由。routes->next未初始化。@BLUEPIXY第二次在
else
中,或者我盯着它看的时间太长了?@tjb1982
malloc
不会将内存归零,因此需要将链接列表中最后一个节点的
next
设置为空。您在任何地方都没有这样做。您需要在通过
malloc
创建
struct route
时初始化所需的成员。谢谢!成功了。但我真的不明白为什么。你能解释一下吗?访问无效指针是未定义的行为。由于未将next设置为NULL,因此从第二次调用
register\u route
开始遍历列表是未定义的。谢谢!成功了。但我真的不明白为什么。你能解释一下吗?访问无效指针是未定义的行为。由于未将next设置为NULL,因此未定义从第二次调用
register\u route
开始遍历列表。