【点滴记录】free函数的作用

今天看一段程序,C真是少用。如下,是双向链表的清空操作:

void bilistClear(Node *bl){
	if(bl->data!=NULL){
		free((char*)bl->data);
		bl->data=NULL;
	}
	if(bl->pre!=NULL)
		bl->pre->next=NULL;
	bl->pre=NULL;
	if(bl->next==NULL)
		return;
	bilistClear(bl->next);
	free(bl->next);
	bl->next=NULL;
}

发现,free后还要置为NULL。

以下是MSDN关于free的解释:

The free function deallocates a memory block (memblock) that was previously allocated by a call to callocmalloc, or realloc. The number of freed bytes is equivalent to the number of bytes requested when the block was allocated (or reallocated, in the case ofrealloc). If memblock is NULL, the pointer is ignored and free immediately returns. Attempting to free an invalid pointer (a pointer to a memory block that was not allocated by callocmalloc, or realloc) may affect subsequent allocation requests and cause errors.

If an error occurs in freeing the memory, errno is set with information from the operating system on the nature of the failure. For more information, see errno, _doserrno, _sys_errlist, and _sys_nerr.

After a memory block has been freed, _heapmin minimizes the amount of free memory on the heap by coalescing the unused regions and releasing them back to the operating system. Freed memory that is not released to the operating system is restored to the free pool and is available for allocation again.

When the application is linked with a debug version of the C run-time libraries, free resolves to _free_dbg. For more information about how the heap is managed during the debugging process, see The CRT Debug Heap.

free is marked __declspec(noalias), meaning that the function is guaranteed not to modify global variables. For more information, seenoalias.

To free memory allocated with _malloca, use _freea.

看来free只是表示此处内存可供allocateFreed memory that is not released to the operating system is restored to the free pool and is available for allocation again.而之前的值并没有被清空。因此为了某些安全考虑free后置其值为NULL。如果它的值还有用,那再另说了。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值