我想知道在哪里必须使用“免费”功能?
当我已经使用“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/