malloc(0)有什么意义?

编程入门 行业动态 更新时间:2024-10-17 10:36:01
本文介绍了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)有什么意义?

本文发布于:2023-11-02 16:54:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552918.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有什么意义   malloc

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!