C语言 C 中 free() 函数的正确用法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29444971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Correct usage of free() function in C
提问by DDeme
I am new in C programming language so can you tell me if this is correct way to do.
我是 C 编程语言的新手,所以你能告诉我这是否是正确的方法。
for example:
program points on buffer and i use that pointeras parameter in free()function. So, what problems can this function cause ?
例如:缓冲区上的程序点,我将其pointer用作free()函数中的参数。那么,这个函数会导致什么问题呢?
回答by giorgim
You call free on pointers which have been assigned memory returned by malloc/calloc/realloconly.
您对已分配内存的指针调用 freemalloc/calloc/realloc仅由 返回。
E.g.
例如
char* ptr=malloc(10);
// use memory pointed by ptr
// e.g., strcpy(ptr,"hello");
free(ptr); // free memory pointed by ptr when you don't need it anymore
Things to remember:
要记住的事情:
Never free memory twice. This can be done: (1) If you call
freeonptrtwice and value ofptrwasn't changed since first call tofree(2) You have two different pointers pointing to same memory; If you call free on one, you are not allowed to callfreeon the second pointer now tooWhen you free a pointer you are not even allowed to readits value; e.g.,
if (ptr)not allowed after freeing unless you initializeptrto a new valueOf course you should not also dereference freed pointer
As pointed out by @chqrlie, I will also add here that it is also perfectly OK to pass a null pointer to
free, which will just do nothing then
永远不要两次释放内存。这可以做到:(1)如果你调用
free了ptr两次并且ptr自第一次调用以来的值没有改变free(2)你有两个不同的指针指向同一个内存;如果你免费呼叫一个,你现在也不允许呼叫free第二个指针当你释放一个指针时,你甚至不能读取它的值;例如,
if (ptr)除非您初始化ptr为新值,否则在释放后不允许当然,您也不应该取消引用已释放的指针
正如@chqrlie 所指出的,我还要在这里补充一点,将空指针传递给 也是完全可以的,
free然后什么都不做
回答by Paul Ogilvie
Think that the computer has a whole bunch of memory not (yet) used by your program. Now you need some more memory and you ask your computer to give you some more (for example, a large buffer). Once you are done with it, you want to return it to the computer.
认为计算机有一大堆内存(尚未)被您的程序使用。现在您需要更多内存,并要求计算机提供更多内存(例如,大缓冲区)。完成后,您想将其返回到计算机。
This memory is called the heap. You ask for memory by calling malloc()and you return it by calling free();
这种内存称为堆。您通过调用请求内存并通过调用malloc()返回它free();
char *buffer;
buffer = malloc(512); // ask for 512 bytes of memory
if (buffer==NULL) return -1; // if no more memory available
...
free(buffer); // return the memory again
回答by Vipul Dimri
free()function is used to deallocate memory used by one program and move it back to available memory area so that other operating system processes can use that memory location. Also freefunction takes any type of pointer that points to that memory location.
For example:
free()函数用于释放一个程序使用的内存并将其移回可用内存区域,以便其他操作系统进程可以使用该内存位置。还free函数采用任何类型的指针指向该存储器位置。例如:
int a = 10; // suppose 2 byte is allocated ie location 1000 and 1001
Now this 2 byte of memory belongs to specific problem; hence OS will not give this memory location to another process (memory is now allocated memory not available memory)
现在这2字节的内存属于特定问题;因此操作系统不会将此内存位置提供给另一个进程(内存现在已分配内存而不是可用内存)
int *ptr =&a;
/*ptr is pointer variable pointing to 1000
as it is int pointer therefore ptr++ will move pointer to 1002*/
Now if we do free(ptr), it will check the pointer type and depending on type free function deallocate memory in this case 2 bytes starting from 1000.
现在,如果我们这样做free(ptr),它将检查指针类型并根据类型释放函数在这种情况下从 1000 开始释放 2 个字节的内存。
Now interesting point is your data will be there until OS allocates this memory to some other process and that process overwrites it.
现在有趣的一点是,您的数据将一直存在,直到操作系统将此内存分配给其他某个进程并且该进程将其覆盖。
Also ptris pointing to 1000 even after free()function but that memory location does not belong to our program hence ptrpointer has given new name DANGLING POINTER.
ptr即使在free()函数之后也指向 1000但该内存位置不属于我们的程序,因此ptr指针赋予了新名称DANGLING POINTER。
*ptrmay or may not give the same value therefore it is better to make ptr =null.
*ptr可能会或可能不会给出相同的值,因此最好 make ptr =null。
回答by Karthikeyan.R.S
From the man pageof free()function:
从手册页的free()功能:
The
free()function frees the memory space pointed to by a pointerptrwhich must have been returned by a pre‐ vious call tomalloc(),calloc()orrealloc(). Otherwise, or iffree(ptr)has already been called before, undefined behavior occurs. IfptrisNULL, no operation is performed.
该
free()函数释放一个指针所指向的内存空间,该指针ptr必须由先前对malloc()、calloc()或 的调用返回realloc()。否则,或者如果free(ptr)之前已经调用过,则会发生未定义的行为。如果ptr是NULL,则不执行任何操作。
You have to use the free()function when you are allocating the memory dynamically.
free()动态分配内存时必须使用该函数。
If you are using that as a static variable then it may lead to unintended behavior.
如果您将其用作静态变量,则可能会导致意外行为。
char *c=malloc(100);//allocating the 100 bytes of memory for a pointer variable c.
Here after usage of that varaible you can free that allocated memory,
在使用该变量后,您可以释放分配的内存,
free(c);
If you are declared a variable like this,
如果你被声明为这样的变量,
char c= malloc(100);// It is illegeal. And c will have a memory in stack.
If you free this variable,
如果你释放这个变量,
free(c);//it will lead to system crash. Because your freeing the memory which is in stack memory.

