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

free

void free (void* ptr);
Deallocate memory block

A block of memory previously allocated by a call to malloccalloc 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.

Parameters

ptr
Pointer to a memory block previously allocated with malloccalloc or realloc.

Return Value

none

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
/* 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 stdlibfunctions.

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 or malloc), the functions are synchronized in such a way that the deallocation happens entirely before the next allocation.

Exceptions (C++)

No-throw guarantee: this function never throws exceptions.

If ptr does not point to a memory block previously allocated with malloccalloc or realloc, and is not a null pointer, it causes undefined behavior.

See also

malloc
Allocate memory block (function )
calloc
Allocate and zero-initialize array (function )
realloc
Reallocate memory block (function )

free 释放内存的更多相关文章

  1. CentOS7清理yum缓存和释放内存方法

    清理yum缓存 清理yum缓存使用yum clean 命令,yum clean 的参数有headers, packages, metadata, dbcache, plugins, expire-ca ...

  2. 在dll里malloc/new/cvCreate分配内存,在exe里free/Releases释放内存时会出错。

    写了个程序,在DLL中用malloc分配了一块内存,但是在exe程序中释放,结果程序crash,原因就是:其原因可能是堆被损坏,这也说明 TestMySticker.exe 中或它所加载的任何 DLL ...

  3. PHP unset()函数销毁变量 但没有实现释放内存

    <?PHP $a = "hello";$b = &$a;unset( $b );echo $a; // 输出 helloecho $b; // 报错$b = &quo ...

  4. PHP unset销毁变量并释放内存

    PHP的unset()函数用来清除.销毁变量,不用的变量,我们可以用unset()将它销毁.但是某些时候,用unset()却无法达到销毁变量占用的内存!我们先看一个例子: <?php $s=st ...

  5. vector容器删除某些元素且释放内存

    1,size和capacity size: 指目前容器中实际有多少元素,对应的resize(size_type)会在容器尾添加或删除一些元素,来调整容器中实际的内容,使容器达到指定的大小. capac ...

  6. android回收AnimationDrawable动画的每一帧的图片资源,而释放内存

    回收每一帧的图片,释放内存资源 private void tryRecycleAnimationDrawable(AnimationDrawable animationDrawables) { if ...

  7. Linux手动释放内存

    手动释放内存 1.sync将内存中的缓存写入磁盘 2.  to free pagecache, use echo 1 > /proc/sys/vm/drop_caches;       to f ...

  8. redis采用tcmalloc导致无法释放内存的问题

    from:http://wangneng-168.iteye.com/blog/2100379 redis使用tcmalloc管理内存,当删除了redis的key后,通过redis的info命令查看内 ...

  9. python里的del变量无法立刻释放内存的解决办法

    最近在python开发的时候,用到了一些很占用内存的操作,导致后续程序执行很慢甚至无法执行.探索了一下,最终解决了这个问题. 截图解释: python变量占用了内存,仅仅通过del变量的方式,只是让这 ...

  10. /MT、/MD编译选项,以及可能引起在不同堆中申请、释放内存的问题

    一.MD(d).MT(d)编译选项的区别 1.编译选项的位置 以VS2005为例,这样子打开: 1)         打开项目的Property Pages对话框 2)         点击左侧C/C ...

随机推荐

  1. actor mysql 持久化之 specified actor

    持久化到mysql,要求一次操作涉及到的多次读写的事务性.使用的 library 是 postgresql-async, akka 版本是 2.11. 1. 实现 per-user 逻辑,简单来讲,就 ...

  2. Spring集成Mybatis,spring4.x整合Mybatis3.x

    Spring集成Mybatis,spring4.x整合Mybatis3.x ============================== 蕃薯耀 2018年3月14日 http://www.cnblo ...

  3. Perl操作Oracle

    一. perl连接Oracle数据库 [oracle@oracle11gR2 perl_script]$ more connect.pl #!/usr/bin/perl #perl script us ...

  4. [Z]修炼成C++高手必看的C++书单

    增添于网上的一些书单: C++/OPP/OOD系列: 层级一:语法/语意(C++) [Lippman2000] Essential C++ Essential C++,by Stanley B. Li ...

  5. 在apache虚拟目录配置

    在apache虚拟目录配置中 <VirtualHost *:80>xxx xxx xxx</VirtualHost> 不能写成 <VirtualHost *>xxx ...

  6. 解析pdb文件得到未导出变量地址(转)

    程序要用到dbghelp.dll中的一些函数 http://msdn.microsoft.com/en-us/library/ms679291%28VS.85%29.aspx 要自己下载系统对应的符号 ...

  7. Foxmail邮箱最新应用指南二

    Foxmail邮箱最新应用指南二 1.打开Foxmail主界面—工具—账号管理,或者鼠标右击任何已有账号—属性,弹出账号管理窗口,点击左下角的“新建”按钮: 2.输入邮箱地址,下一步→选择邮箱类型(I ...

  8. WEB中会话跟踪[转]

    今天晚上去华工参加睿智融科的笔试,问到web会话跟踪,一脸懵比,这个词听都没听过,回来后百度下,发现其实会话跟踪的内容我基本都了解的~_~ 转自:http://www.cnblogs.com/gaop ...

  9. 通俗大白话来理解TCP协议的三次握手和四次分手

    通俗理解: 但是为什么一定要进行三次握手来保证连接是双工的呢,一次不行么?两次不行么?我们举一个现实生活中两个人进行语言沟通的例子来模拟三次握手. 引用网上的一些通俗易懂的例子,虽然不太正确,后面会指 ...

  10. 原生js--异步请求

    1.异步请求的方法: iframe.script.XMLHttpRequest.comet(服务器端发起) 2.XMLHttpRequest request = new XMLHttpRequest( ...