零大小的malloc [英] zero size malloc
本文介绍了零大小的malloc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
很简单的问题,我提出了以下程序:
Very simple question, I made the following program :
#include <stdlib.h>
int main(int argc, char ** argv)
{
void * ptr;
ptr = malloc(0);
free(ptr);
}
和它不段错误我的机器上。难道STDLIB malloc和free,或便携式行为我在自找麻烦?
And it does not segfault on my machine. Is it a portable behaviour of stdlib malloc and free, or am I looking for trouble ?
编辑:什么似乎非移植是由malloc返回的值。现在的问题是有关的malloc(0)+自由组合,而不是PTR的值。
Edit : What seems non portable is the value returned by malloc. The question is about the malloc(0) + free combination, not the value of ptr.
推荐答案
的行为是实现定义,您将收到要么一个空指针或地址。对所接收的指针调用free应该但是不会造成问题,因为:
The behaviour is implementation defined, you will receive either a NULL pointer or an address. Calling free for the received pointer should however not cause a problem since:
- 免费(NULL)是确定的,没有操作完成
- 免费(地址)是确定的,如果从malloc的接收地址(或其他类似释放calloc等)
这篇关于零大小的malloc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文