为什么我在使用指针后必须释放它??? (ANSI-C)
我读到了这个: http://www.drpaulcarter.com/cs/common -c-errors.php#2.8 有这样的代码块:
#include <string.h>
#include <stdlib.h>
int main()
{
char *st = malloc(20); /* st points to allocated array*/
strcpy(st, "abc"); /* st points to char array */
free(st); /* don't forget to deallocate when done! */
return 0;
}
I read this: http://www.drpaulcarter.com/cs/common-c-errors.php#2.8
and there's a block of code like that:
#include <string.h>
#include <stdlib.h>
int main()
{
char *st = malloc(20); /* st points to allocated array*/
strcpy(st, "abc"); /* st points to char array */
free(st); /* don't forget to deallocate when done! */
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在大多数正常的操作系统上,您实际上不必在退出时进行清理 - 这只是一个好习惯。
该代码只是分配和释放内存的简化示例,在真实的程序中,您将跟踪程序运行时需要释放的内存,否则您将耗尽内存(或更可能的地址空间)
On most sane operating systems you don't really have to clean up on exit - it's just a good habit.
The code is just a simplified example of allocating and freeing memory, in a real program you would keep track of memory that needed freeing while the program was running otherwise you would run out of memory (or more likely address space)
您必须释放它(通过
free()
),因为您已经分配了它(通过malloc()
)。在大多数 C API 中,如果分配了指针,通常您有责任释放它。在某些情况下,情况并非如此,但函数的手册页通常会指定这一点。You have to deallocate it (via
free()
) because you allocated it (viamalloc()
). In most C APIs, generally you are responsible for freeing a pointer if you allocate it. There are some cases in which this isn't true, but the function's man page will usually specify that.分配和解除分配内存的影响。一旦您出于某种目的分配了内存,它就会保持分配状态,直到您解除分配为止。虽然已分配,但不可用于其他目的。释放它的原因是为了使该内存可用于其他用途。
为什么这很重要。如果您的程序随着时间的推移继续分配新的内存块,这一点就变得很重要。如果在不再需要内存时不释放内存,程序对内存的使用会不断增加,最终可能会耗尽操作系统提供给它的内存池。
在此示例中没有真正的危害。在您的特定示例中,跳过释放(可能)没有任何危害。一方面,程序此后立即结束,因此程序不再使用该内存。另一方面,正如其他人提到的,在 C 的典型实现中,程序在退出时会归还所有内存。
但是,如果您的程序可能需要内存用于其他用途,或者您的程序在退出时不会将内存返还给操作系统,那么最好在不再需要内存时释放内存。
释放的是内存,而不是指针。严格来说,该示例并没有释放指针。它释放指针指向的内存。指针仍然在那里,实际上仍然指向现在已释放的内存。如果您无意中继续使用指针,就好像它仍然指向它拥有的内存一样,这可能会带来无尽的调试乐趣。
The effects of allocating and deallocating memory. Once you allocate memory for some purpose, it stays allocated until you deallocate it. While it's allocated, it is not available for other purposes. The reason to deallocate it is to make that memory available for other uses.
Why this matters. This becomes important if your program continues to allocate new chunks of memory over time. If you don't deallocate memory when you no longer need it, your program's use of memory grows and grows and grows, and may eventually exhaust the pool of memory that the operating system makes available to it.
No real harm in this example. In the particular example you, there's (likely) no harm in skipping the deallocation. For one thing, the program ends immediately thereafter, so the program has no further uses for that memory. For another thing, as others have mentioned, in typical implementations of C, your program gives back all of its memory when it exits.
But if your program might need the memory for something else, or if your program won't give the memory back to the operating system when it exits, it's a good idea to deallocate memory when you no longer need it.
The memory, not the pointer, is deallocated. Strictly speaking, that example does not deallocate the pointer. It deallocates the memory that the pointer points to. The pointer is still there, and in fact is still pointing to the now-deallocated memory. This can lead to endless debugging joy if you inadvertently continue to use the pointer as if it still pointed to memory it owns.
不能保证分配的内存会因为进程终止而返回到系统资源。
There is no guarantee that the allocated memory will go back to the system's resources just because the process terminated.
在 main() 函数中,一旦程序退出,所有分配的内存都会返回给操作系统,即使您没有显式地释放它。因此,手动执行通常被称为良好实践,这种“良好实践”具有实际意义。如果您将此代码放在另一个函数中,甚至在 main 中并在循环中使用,您的程序将根据代码分配和释放内存。如果您的代码处于循环中并且没有释放,这最终会耗尽您的所有内存,导致您的程序崩溃,除非进行处理。在某些情况下,这称为内存泄漏。因此,请充分了解良好做法!
内存泄漏
In the main() function, once the program exits, all of the memory allocated is returned back to the OS even if you don't deallocate it explicitly. So doing it manually is generally known as a good practice, this "good practice" has real implications. If you were to have this code in another function or even inside main and with in a loop, your program would allocate and deallocate the memory as per code. If you had the code in a loop and with no deallocations, this would eventually eat all your memory, causing your program to crash unless handled. In certain conditions this is called a memory leak. So, know the good practices well!
Memory Leak
否则,C 内存分配器不知道您已使用完它。
Otherwise the C memory allocator does not know that you are done using it.