是否有必要类型转换malloc和释放calloc [英] is it necessary to type-cast malloc and calloc
问题描述
可能重复:结果
难道我投malloc的结果?
块引用>我在谷歌上搜索,找出的malloc 和释放calloc 的类型转换的原因。但是,我只找到的的类型转换的的malloc 是没有必要的,因为它返回void指针的,但怎么样的释放calloc 。这是一样的道理的释放calloc 太???
现在,如果我们搬回到第一点,关于返回值的的malloc 和释放calloc 。后来,我发现,的两者都返回分配空间的。所以,我有点困惑在这里。所以,我的问题是
什么是返回值的malloc 和释放calloc
是否有必要类型转换的的malloc 和释放calloc 。为什么?
块引用>解决方案
什么是malloc和释放calloc的返回值?
块引用>这是一个指向
无效
(无效*
)。
是否有必要类型转换malloc和释放calloc。为什么?
块引用>没有,因为从一个指针转换
无效
的指针对象是隐含的。
C11(n1570),第6.3.2.3指针结果
一个指向无效
可转换为或从指针到任何对象类型。
块引用>这是正确的,对于
的malloc
和释放calloc
。Possible Duplicate:
Do I cast the result of malloc?I was googling to find out the reason for type-casting of malloc and calloc. But, i only found type-casting of malloc is not necessary since it return void pointer but, what about calloc. This is the same reason for calloc too ???
Now, if we move back to first point, about return value of malloc and calloc. Then, i found that, both are returning the allocated spaces. So, i'm little bit confused here. So, my questions are
What is the return value of malloc and calloc
Is it necessary to type-cast malloc and calloc. And why ?
解决方案What is the return value of malloc and calloc?
It is a pointer to
void
(void*
).Is it necessary to type-cast malloc and calloc. And why ?
No, because the conversion from a pointer to
void
to a pointer to object is implicit.C11 (n1570), § 6.3.2.3 Pointers
A pointer tovoid
may be converted to or from a pointer to any object type.It is right for both
malloc
andcalloc
.这篇关于是否有必要类型转换malloc和释放calloc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!