本文介绍了malloc(0)有什么意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我刚刚看到了这个代码:
artist = (char *) malloc(0);...我想知道为什么要这样做吗?
...and I was wondering why would one do this?
推荐答案根据规范, malloc(0)将返回可以成功传递给free()的空指针或唯一指针".
According to the specifications, malloc(0) will return either "a null pointer or a unique pointer that can be successfully passed to free()".
这基本上让您什么也不分配,但是仍然不用担心将"artist"变量传递给对free()的调用.出于实际目的,它与执行操作几乎相同:
This basically lets you allocate nothing, but still pass the "artist" variable to a call to free() without worry. For practical purposes, it's pretty much the same as doing:
artist = NULL;更多推荐
malloc(0)有什么意义?
发布评论