简体   繁体   English

我在函数中分配内存,并返回char *,我应该如何释放它?

[英]I allocate memory in function, and return char*, how should I free it?

If I want to allocate memory in function: 如果我想在函数中分配内存:

char* allocate() {
    char *cp = (char*)malloc(10);
    ...
    return cp;
}

can I use the returned content in cp from main() ? 我可以在main() cp使用返回的内容吗? and how to free cp ? 以及如何释放cp

can I use the returned content in cp in main()? 我可以在main()中的cp中使用返回的内容吗?

Yes, you can. 是的你可以。

and how to free cp? 以及如何释放cp?

Using 运用

free(cp); // Replace cp with the name of the pointer if you are using it in another function


Also, you should not cast the result of malloc (and family) in C 此外,您不应该在C中malloc (和family)的结果

You should always test against failure of malloc(3) , so at least use perror(3) & exit(3) like: 你应该总是测试malloc(3)的失败,所以至少使用perror(3)exit(3),如:

  char *cp = malloc(10);
  if (!cp) { perror("malloc"); exit(EXIT_FAILURE); };

Some Linux systems are enabling memory overcommit . 一些 Linux系统正在启用内存过量使用 This is a dirty feature that you should disable. 这是一个您应该禁用的脏功能。

If you are coding seriously (eg a robust library to be used by others) you may need to provide more sophisticated out-of-memory error recovery (perhaps having a convention that every allocating routine could return NULL on failure). 如果您正在认真编码(例如,其他人使用的健壮库),您可能需要提供更复杂的内存不足错误恢复(可能有一个约定,即每个分配例程在失败时都会返回NULL )。 YMMV. 因人而异。

Indeed, at some later point you need to call free(3) with the pointer in cp . 实际上,稍后您需要使用cp的指针调用free(3) If you don't you'll have a memory leak . 如果你不这样做,你会有内存泄漏 After that you are not allowed to use ie dereference that pointer (be careful about pointer aliases ) or simply compare it against some other pointer; 之后你不允许使用即取消引用指针(注意指针别名 )或简单地将它与其他指针进行比较; it would be undefined behavior . 这将是未定义的行为

It is important to have a convention about when and who should call free . 重要的是要有一个关于何时和谁应该free打电话的约定 You should document that convention. 你应该记录那个惯例。 Defining and implementing such conventions is not always easy. 定义和实施此类约定并非易事。

On Linux you have quite useful tools to help about C dynamic memory allocation : valgrind , passing -Wall -Wextra -g (to get all warnings and debug info) to the GCC compiler, passing -fsanitize=address to a recent gcc , etc. 在Linux上你有很多有用的工具可以帮助你进行C动态内存分配valgrind ,将-Wall -Wextra -g (以获取所有警告和调试信息)传递给GCC编译器,将-fsanitize=address传递给最近的gcc等。

Read also about garbage collection ; 阅读有关垃圾收集的内容 ; at least to get some terminology, like reference counting . 至少得到一些术语,如引用计数 In some C applications, you might later want to use Boehm's conservative GC . 某些 C应用程序中,您可能以后想要使用Boehm的保守GC

1. can I use the returned content in cp in main()? 1.我可以在main()中的cp中使用返回的内容吗?

Yes, you can. 是的你可以。 The scope and lifetime of dynamically allocated memory (on heap) is not limited to the function scope. 动态分配的内存(在堆上)的范围和生命周期不限于函数范围。 It remains valid untill it is free() d. 它仍然有效,直到它是free() d。

Note: Always check for the success for malloc() and do not cast it's return value. 注意:始终检查malloc()是否成功,并且不要强制转换它的返回值。

2. and how to free cp? 2.以及如何释放cp?

Once you've done using the memory, from main() , or from any other function, you can call free(<pointer>); 一旦你使用了内存,从main()或任何其他函数,你可以调用free(<pointer>); , where <pointer> is the variable in which you had collected the return value of allocate() . ,其中<pointer>是您收集了allocate()的返回值的变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM