释放NULL指针 [英] Free a NULL pointer

查看:209
本文介绍了释放NULL指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
在ptr为NULL的情况下,free(ptr)会损坏内存吗?
在其中释放NULL指针是否是一种好习惯C?

Possible Duplicate:
Does free(ptr) where ptr is NULL corrupt memory?
Is it good practice to free a NULL pointer in C?

我有一个关于释放空指针的问题.

I have a question concerning freeing a null pointer.

char *p = NULL;
free(p);

free(NULL)会导致崩溃吗?

还是取决于编译器?

推荐答案

来自免费的手册页

void  free(void *ptr);

free()函数取消分配由指向的内存分配 点如果ptr是NULL指针,则不执行任何操作.

The free() function deallocates the memory allocation pointed to by ptr. If ptr is a NULL pointer, no operation is performed.

如果您想从C手册本身获得确认

If you want to get confirmation from C manual itself

free函数使ptr指向的空间为 释放,即可供进一步分配.如果ptr是 空指针,不执行任何操作.

The free function causes the space pointed to by ptr to be deallocated, that is, made available for further allocation. If ptr is a null pointer, no action occurs.

请参见文档的第313页

See page 313 of this document.

这篇关于释放NULL指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆