c语言 - 为什么"free"这样说我的指针无效?


while(n >= 0);
    node * point = malloc(sizeof(node));
    point = create(number, n);
    //show(point);
    //free memory
    while(point != NULL)
    {
        node *tmp = point->next;
        free(point);
        point = tmp;
    }
}
node * create(int data[], int len)
{
    node * list_head;
    node * list;
    for (int i = 0; i < len; i++)
    {
        if(i == 0)
        {
            list_head = malloc(sizeof(node));
            list = malloc(sizeof(node));
            list_head->value = data[0];
            list_head->next = list;
        }
        else
        {
            list->value = data[i];
            list->next = malloc(sizeof(node));
            list->next = NULL;
        }
    }
    return list_head;
}

此代码返回一个错误,该错误为:-free((:无效指针中止|为什么我的代码没有释放我分配的内存。是因为我把它分配到了不同的功能中吗?

您发布的代码应更改为:

while(n >= 0) {
    node * point = create(number, n);

和:

list->next = NULL;

应该改为

list = list->next;

最新更新