realloc

本文深入探讨了realloc函数的功能,包括如何改变指针所指向内存块的大小,是否移动内存块及其可能带来的影响,以及realloc在不同场景下的行为,如ptr为NULL时的行为和size为0时的内存释放。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

realloc

<cstdlib>
void * realloc ( void * ptr, size_t size );
Reallocate memory block
The size of the memory block pointed to by the  ptr  parameter is changed to the  size  bytes, expanding or reducing the amount of memory available in the block.

The function may move the memory block to a new location, in which case the new location is returned. The content of the memory block is preserved up to the lesser of the new and old sizes, even if the block is moved. If the new  size  is larger, the value of the newly allocated portion is indeterminate.

In case that  ptr  is NULL, the function behaves exactly as  malloc , assigning a new block of  size  bytes and returning a pointer to the beginning of it.

In case that the size is 0, the memory previously allocated in ptr is deallocated as if a call to free was made, and a NULL pointer is returned.

Return Value

A pointer to the reallocated memory block, which may be either the same as the  ptr  argument or a new location.
The type of this pointer is  void* , which can be cast to the desired type of data pointer in order to be dereferenceable.
If the function failed to allocate the requested block of memory, a NULL pointer is returned, and the memory block pointed to by argument  ptr  is left unchanged.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值