什么是在的malloc(0)的点? [英] what's the point in malloc(0)?

查看:100
本文介绍了什么是在的malloc(0)的点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚看到这个 code:

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()".

这基本上让你分配什么,但仍然是艺术家变量传递给调用free()无后顾之忧。出于实用的目的,这是pretty一样一样做的:

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)的点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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