malloc(0) 的意义是什么? [英] What's the point of malloc(0)?

查看:36
本文介绍了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) 的意义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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