Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C-链表:free()函数用于删除我的头部节点;如何正确使用free()?_C_Pointers_Linked List_Segmentation Fault - Fatal编程技术网

C-链表:free()函数用于删除我的头部节点;如何正确使用free()?

C-链表:free()函数用于删除我的头部节点;如何正确使用free()?,c,pointers,linked-list,segmentation-fault,C,Pointers,Linked List,Segmentation Fault,我需要做一个函数,在c中删除链表的前n个节点,并返回删除的节点数。如果列表小于n,则应为空。此外,我不能使用递归性 现在的代码可以正常工作,但我不会释放“已删除”节点的内存。如果我取消注释应该释放内存的部分,则会在codeboard.io上出现以下错误: Input: 5 + [ 61 62 63 64 65 66 ] Output: expected 5 + [ 66 ] obtained 5 + [19333664 ] 这个随机数似乎是在访问内存中的“垃圾”。 如何正确释放

我需要做一个函数,在c中删除链表的前n个节点,并返回删除的节点数。如果列表小于n,则应为空。此外,我不能使用递归性

现在的代码可以正常工作,但我不会释放“已删除”节点的内存。如果我取消注释应该释放内存的部分,则会在codeboard.io上出现以下错误:

Input: 5 + [ 61 62 63 64 65 66 ]
Output: expected 5 + [ 66 ]
        obtained 5 + [19333664 ]
这个随机数似乎是在访问内存中的“垃圾”。 如何正确释放不再使用的节点

listas.h中的代码:

typedef struct lligada {
    int valor;
    struct lligada *prox;
} *LInt;

LInt newLInt (int, LInt);

int drop (int, LInt *);
listas.c中的代码

#include <stdlib.h>
#include "listas.h"

int drop (int n, LInt *l){
    int count = 0;
    LInt *aux;
    while(n>0 && (*l)!=NULL){
          n--;
          count++;
          //aux = &((*l));
          *l = (*l)->prox;
          //free(*aux);
    }
return count;
}
#包括
#包括“listas.h”
整数下降(整数n,整数*l){
整数计数=0;
皮棉*aux;
而(n>0&(*l)!=NULL){
n--;
计数++;
//aux=&(*l));
*l=(*l)->prox;
//免费(*aux);
}
返回计数;
}

代码板链接到练习:

我通过不将aux设为双指针,得出了与jboockmann类似的解决方案,但正如我在他的解决方案中所说的,我不明白这是错的,也不明白为什么

int drop (int n, LInt *l){
    int count = 0;
    LInt aux;
    while(n>0 && (*l)!=NULL){
          n--;
          count++;
          aux = *l;
          *l = (*l)->prox;
          free(aux);
        }
    return count;
}

我通过不让aux成为双指针,得到了与jboockmann类似的解决方案,但正如我在他的解决方案中所说的,我不明白这是错的,也不明白为什么是错的

int drop (int n, LInt *l){
    int count = 0;
    LInt aux;
    while(n>0 && (*l)!=NULL){
          n--;
          count++;
          aux = *l;
          *l = (*l)->prox;
          free(aux);
        }
    return count;
}

请注意,
LInt
定义为指向
struct lligada
的指针。因此,
drop
函数的
l
参数是指向
struct lligada
的指针。让我们调用
LInt
变量,该变量
l
指向
list\u head

因此,该行:

aux = &((*l));
实际上是给
aux
分配
list\u head
的地址,而不是
list\u head
指向的
struct lligada

因此,解决方案是将
aux
定义为
LInt
,然后执行以下操作:

aux = *l;
*l = (*l)->prox;
free(aux);

希望能有所帮助。

请注意,
LInt
定义为指向
struct lligada
的指针。因此,
drop
函数的
l
参数是指向
struct lligada
的指针。让我们调用
LInt
变量,该变量
l
指向
list\u head

因此,该行:

aux = &((*l));
实际上是给
aux
分配
list\u head
的地址,而不是
list\u head
指向的
struct lligada

因此,解决方案是将
aux
定义为
LInt
,然后执行以下操作:

aux = *l;
*l = (*l)->prox;
free(aux);

希望能有所帮助。

我在写答案时忽略了一个事实,即
l
是一个双指针。对不起。一般来说,建议使用普通指针而不是多级指针。好的,明白了。我想我现在明白了!强烈建议:不要通过“typedef”隐藏“指针”。而是使用typedef为结构指定一个“短”名称,然后在声明结构实例时,将其设置为指针。为了便于阅读和理解:变量名应该有意义,('l'和'n'没有意义)变量名应该指示内容或用法(或者更好,两者都有)。在
drop()中
函数此行:
LInt*aux应该导致编译器输出警告消息。因为既没有设置也没有使用变量
aux
。编译时,始终启用所有警告,然后修复这些警告。(对于
gcc
,至少使用:
-Wall-Wextra-pedantic
我也使用:
-Wconversion-std=gnu99
)我在写我的答案时忽略了
l
是一个双指针的事实。对不起。一般来说,建议使用普通指针而不是多级指针。好的,明白了。我想我现在明白了!强烈建议:不要通过“typedef”隐藏“指针”。而是使用typedef为结构指定一个“短”名称,然后在声明结构实例时,将其设置为指针。为了便于阅读和理解:变量名应该有意义,('l'和'n'没有意义)变量名应该指示内容或用法(或者更好,两者都有)。在
drop()中
函数此行:
LInt*aux应该导致编译器输出警告消息。因为既没有设置也没有使用变量
aux
。编译时,始终启用所有警告,然后修复这些警告。(对于
gcc
,至少使用:
-Wall-Wextra-pedantic
我也使用:
-Wconversion-std=gnu99