c - 免费功能的使用

标签 c pointers linked-list

我想知道在哪里必须使用“免费”功能?

当我已经使用“malloc”函数时我是否使用它,或者我将它与任何声明的指针一起使用?

注意:在这两种情况下,liste 类型定义如下:

typedef struct Node * liste; 

Node 也是一个结构:

struct Node { 
    int value; 
    Node *N;
};

第一种情况

liste l; 
free (l);

第二种情况

liste l;
l=(node*)malloc(sizeof(node));
free (l);

提前谢谢您!

最佳答案

man page for free声明如下:

The free() function frees the memory space pointed to by ptr, which must have been returned by a previous call to malloc(), calloc() or realloc(). Otherwise, or if free(ptr) has already been called before, undefined behavior occurs. If ptr is NULL, no operation is performed.

在第一种情况下,使用 free 无效,因为 l 未初始化。第二种情况是有效的(假设 liste 是指针的 typedef),因为 l 被分配了由 malloc 返回的地址。

关于c - 免费功能的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54857762/

相关文章:

c - 如何获取网络设备统计信息?

c - 使用指针从循环数组中弹出

C 命令行参数

c - fscanf 和链表的 char *

带有循环的Scala不可变链表

c - wValue 在通过 USB 与 "USB HID Red Visual Indicator"交互时扮演什么角色?

c - 简单的C scanf 行不通?

C 管道、fork、dup 和 exec()

c - 返回指向局部变量的指针

java - 将两个数字相加,并以反向链表形式存储