是否有必要类型转换malloc和释放calloc [英] is it necessary to type-cast malloc and calloc

查看:297
本文介绍了是否有必要类型转换malloc和释放calloc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能重复:结果
  难道我投malloc的结果?


我在谷歌上搜索,找出的malloc 释放calloc 的类型转换的原因。但是,我只找到的的类型转换的的malloc 是没有必要的,因为它返回void指针的,但怎么样的释放calloc 。这是一样的道理的释放calloc 太???

现在,如果我们搬回到第一点,关于返回值的的malloc 释放calloc 。后来,我发现,的两者都返回分配空间的。所以,我有点困惑在这里。所以,我的问题是


  

      
  1. 什么是返回值的malloc 释放calloc


  2.   
  3. 是否有必要类型转换的的malloc 释放calloc 。为什么?


  4.   


解决方案

  

什么是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

  1. What is the return value of malloc and calloc

  2. 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 to void may be converted to or from a pointer to any object type.

It is right for both malloc and calloc.

这篇关于是否有必要类型转换malloc和释放calloc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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