首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::free

Defined in header <cstdlib>

?

?

void free( void* ptr );

?

?

分配以前由std::malloc(),,,std::calloc()std::realloc()...

如果ptr是一个空指针,函数什么也不做。

如果ptr返回的值不等于std::malloc(),,,std::calloc(),或std::realloc()...

如果ptr已经被取消了,就是,std::free()std::realloc()已经被调用ptr作为争论而不是呼吁std::malloc(),,,std::calloc()std::realloc()导致指针等于ptr之后。

如果是在后面,则行为未定义。std::free()返回时,将通过指针进行访问。ptr%28除非另一个分配函数发生时导致指针值等于ptr29%。

The following functions are required to be thread-safe: The library versions of operator new and operator delete User replacement versions of global operator new and operator delete std::calloc, std::malloc, std::realloc, std::aligned_alloc (since C++17) Calls to these functions that allocate or deallocate a particular unit of storage occur in a single total order, and each such deallocation call happens-before the next allocation (if any) in this order.

(since C++11)

  • 的库版本operator newoperator delete
  • 全局用户替换版本operator newoperator delete
  • std::calloc,,,std::malloc,,,std::realloc,,,std::aligned_alloc%28自C++17%29

对分配或释放特定存储单元的这些函数的调用是以单个总顺序进行的,并且每个这样的释放调用都会发生。发生-之前下一次按此顺序分配%28(如果有%29)。

%28自C++11%29

参数

ptr

-

pointer to the memory to deallocate

返回值

%280%29

注记

该函数接受%28,并且不使用%29空指针来减少特殊大小写的数量。无论分配成功与否,都可以将分配函数返回的指针传递给free()...

二次

代码语言:javascript
复制
#include <cstdlib>
 
int main()
{
    int* p1 = (int*)std::malloc(10*sizeof *p1);
    std::free(p1); // every allocated pointer must be freed
 
    int* p2 = (int*)std::calloc(10, sizeof *p2);
    int* p3 = (int*)std::realloc(p2, 1000*sizeof *p3);
    if(p3) // p3 not null means p2 was freed by std::realloc
       std::free(p3);
    else // p3 null means p2 was not freed
       std::free(p2);
}

二次

另见

c免费文件

*。

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com