free 释放内存

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

 

posted @   papering  阅读(412)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
历史上的今天:
2018-01-31 重复点击不冲突的坐标点
2018-01-31 a
2018-01-31 获取浏览器弹窗alert、自定义弹窗以及其操作
2018-01-31 头条规则利率 今日头条 规则记录 前端代码 迭代记录
2018-01-31 处理机调度层次
2018-01-31 qq 空间视频地址 的 有效期 403
2018-01-31 Keys.BACKSPACE Keys.SPACE
点击右上角即可分享
微信分享提示