free

free

void free(void *ptr);
1. Deallocate memory block
        A block of memory previously allocated by a call to malloc, calloc or realloc is deallocated, making it available again for further allocations.
        If ptr does not point to a block of memory allocated with the above functions, it causes undefined behavior.
        If ptr is a null pointer, the function does nothing.
        Notice that this function does not change the value of ptr itself, hence it still points to the same (now invalid) location.
1.1 Parameters
        ptr - Pointer to a memory block previously allocated with malloc, calloc or realloc.
1.2 Return Value
        none
1.3 Data races
        Only the storage referenced by ptr is modified. No other storage locations are accessed by the call.
        If the function releases a unit of storage that is reused by a call to allocation functions (such as calloc ormalloc), the functions are synchronized in such a way that the deallocation happens entirely before the next allocation.
1.4 Exceptions (C++)
        No-throw guarantee: this function never throws exceptions.
        If ptr does not point to a memory block previously allocated with malloc, calloc or realloc, and is not a null pointer, it causes undefined behavior.
2. Example
2.1 example

/*
 ============================================================================
 Name        : free_example_1.c
 Author      : foreverstrong
 Version     :
 Copyright   : Your copyright notice
 Description : free_example in C, Ansi-style
 ============================================================================
 */

/* free example */
#include <stdlib.h>     /* malloc, calloc, realloc, free */

int main()
{
	int *buffer1, *buffer2, *buffer3;

	buffer1 = (int*) malloc(100 * sizeof(int));
	buffer2 = (int*) calloc(100, sizeof(int));
	buffer3 = (int*) realloc(buffer2, 500 * sizeof(int));

	free(buffer1);
	free(buffer3);

	return 0;
}

        This program has no output. It just demonstrates some ways to allocate and free dynamic memory using the C stdlib functions.
2.2 example

/*
 ============================================================================
 Name        : free_example_2.c
 Author      : foreverstrong
 Version     :
 Copyright   : Your copyright notice
 Description : free_example in C, Ansi-style
 ============================================================================
 */

/* free example */
#include <stdlib.h>  /* malloc, calloc, realloc, free */

int main()
{
	int *buffer1, *buffer2, *buffer3;

	buffer1 = (int*) malloc(100 * sizeof(int));
	buffer2 = (int*) calloc(100, sizeof(int));
	buffer3 = NULL;

	if (buffer1)
	{
		free(buffer1);
		buffer1 = NULL;
	}

	if (buffer2)
	{
		free(buffer2);
		buffer2 = NULL;
	}

	return 0;
}

IMPORTANT NOTICE:
        对于 free(pointer); 语句,如果 pointer 是 NULL 指针,那么 free 对 pointer 无论操作多少次都不会出问题。如果 pointer 不是 NULL 指针,那么 free 对 pointer 连续操作两次就会导致程序运行错误。malloc 的次数和 free 的次数相等。
        free(pointer); 之后指针仍然指向原来的堆地址,如果继续使用,将会十分危险。因为操作系统已经认为这块内存可以使用,他会毫不考虑的将它分配给其它程序,于是你下次使用的时候可能就已经被别的程序改掉了,这种情况就叫野指针,所以最好 free(pointer); 之后再置空 pointer = NULL; ,表明本程序已经放弃再使用它。
        free(pointer); 释放的是指针指向的内存。注意,释放的是内存,不是指针。指针并没有被释放,指针仍然指向原来的存储空间。指针是一个变量,只有程序结束时才被销毁。释放了内存空间后,原来指向这块空间的指针还是存在,只不过现在指针指向的内容是垃圾,是未定义的。因此,释放内存后把指针指向 NULL,防止指针在后面不小心又被解引用了。
        野指针是指程序员或操作者不能控制的指针。野指针不是 NULL 指针,而是指向垃圾的指针。
造成野指针的原因主要是:
        1. 指针变量没有初始化,任何指针变量刚被创建时不会自动成为 NULL 指针,它的缺省值是随机的,它会乱指一气。在初始化的时候要么指向合法的指针,要么指向 NULL.
        2. 指针变量被 free 或 delete 之后,没有设置为 NULL. 它们只是把指针所指的内存给释放掉,但并没有把指针本身置为 NULL。通常使用语句 if(pointer != NULL); 进行防错处理。此时 if 语句起不到防错作用,因为即便 pointer 不是 NULL 指针,它也不指向合法的内存块。
        3. 指针操作超越了变量的作用范围。注意指针生命周期。

3. free
        定义于头文件 <stdlib.h>
void free(void *ptr);
        解分配之前由 malloc()、 calloc()、 aligned_alloc (C11 起) 或 realloc() 分配的空间。
        若 ptr 为空指针,则函数不进行操作。
        若 ptr 的值不等于之前从 malloc() 、 calloc() 、 realloc() 或 aligned_alloc()  (C11 起) 返回的值,则行为未定义。
        若 ptr 所指代的内存区域已经被解分配,则行为未定义,即是说已经以 ptr 为参数调用 free() 或 realloc() ,而且没有后继的 malloc()、 calloc() 或 realloc() 调用以 ptr 为结果。
        若在 free() 返回后通过指针 ptr 访问内存,则行为未定义 (除非另一个分配函数恰好返回等于 ptr 的值)。

        free 是线程安全的:它表现得如同只访问通过其参数可见的内存区域,而非任何静态存储。
        令 free 解分配内存区域的调用,同步于任何令分配函数分配相同或部分相同区域的后续调用。这种同步出现于任何解分配函数所做的内存访问后,和任何分配函数所做的内存访问前。所有操作每块特定内存区域的分配及解分配函数拥有单独全序。(C11 起)
3.1 Parameters
       ptr - 指向要解分配的内存的指针
3.2 Return Value
        无
IMPORTANT NOTICE:

        此函数接收空指针 (并对其不处理) 以减少特例的数量。不管分配成功与否,分配函数返回的指针都能传递给 free() 。

4. Example

4.1 example

/*
 ============================================================================
 Name        : free_example_3.c
 Author      : foreverstrong
 Version     :
 Copyright   : Your copyright notice
 Description : free_example in C, Ansi-style
 ============================================================================
 */

/* free example */
#include <stdlib.h>  /* malloc, calloc, realloc, free */

int main(void)
{
	int *p1 = malloc(10 * sizeof(*p1));

	if (p1)
	{
		free(p1);
		p1 = NULL;
	}

	int *p2 = calloc(10, sizeof(*p2));
	int *p3 = realloc(p2, 1000 * sizeof(*p3));

	if (p3)
	{
		free(p3);
		p2 = NULL;
		p3 = NULL;
	}
	else
	{
		free(p2);
		p2 = NULL;
	}
}

 

References

http://www.cplusplus.com/

http://zh.cppreference.com/w/%E9%A6%96%E9%A1%B5

http://www.cplusplus.com/reference/cstdlib/free/

http://zh.cppreference.com/w/c/memory/free

http://en.cppreference.com/w/c/memory/free

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yongqiang Cheng

梦想不是浮躁,而是沉淀和积累。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值