当我需要在C语言中投malloc的结果? [英] When do I need to cast the result of malloc in C language?

查看:146
本文介绍了当我需要在C语言中投malloc的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据这个老问题 的malloc 返回一个指向无效

自动,安全地晋升为任何其他指针类型

is automatically and safely promoted to any other pointer type

但阅读K&安培; R我发现这下code

But reading K&R I've found this following code

char *strdup(char *s)
{
char *p;
/* make a duplicate of s */
p = (char *) malloc(strlen(s)+1)

有什么区别?

推荐答案

有关符合C89或更高版本的实现,铸造的结果的malloc()将没有必要。

For any implementation conforming to C89 or later, casting the result of malloc() is never necessary.

铸造的结果的malloc()勘误表的 C程序设计语言(第二版)

142(§6.5,向着终点):关于铸造返回值的备注
  的malloc(正确的方法是声明...那么明确
  强制)需要被重写。这个例子是正确的,工作原理,但
  建议在1988-1989 ANSI / ISO的情况下值得商榷
  标准。这是没有必要(因为无效*胁迫
   ALMOSTANYTYPE * 是自动的),也可能有害,如果的malloc
  代理它,没有被声明为返回无效* 。显式
  投可以掩盖意外的错误。另一方面,$ P $对ANSI,
  剧组是必要的,它是在C ++中也。

142(§6.5, toward the end): The remark about casting the return value of malloc ("the proper method is to declare ... then explicitly coerce") needs to be rewritten. The example is correct and works, but the advice is debatable in the context of the 1988-1989 ANSI/ISO standards. It's not necessary (given that coercion of void * to ALMOSTANYTYPE * is automatic), and possibly harmful if malloc, or a proxy for it, fails to be declared as returning void *. The explicit cast can cover up an unintended error. On the other hand, pre-ANSI, the cast was necessary, and it is in C++ also.

(该链接不再有效。后期丹尼斯里奇的主页是现在这里;它指向的主页上的更新地点对于这本书,但链接也无效。)

(That link is no longer valid. The late Dennis Ritchie's home page is now here; it points to an updated location for the home page for the book, but that link is also invalid.)

最佳做法是指定)一个的malloc的结果(直接打电话到一个指针对象,并让从隐式转换的void * 照顾类型一致性。通过将可见的的malloc()需要声明的#include<文件stdlib.h方式>

Best practice is to assign the result of a malloc() call directly to a pointer object, and let the implicit conversion from void* take care of type consistency. The declaration of malloc() needs to be made visible via #include <stdlib.h>.

(这是几乎没有可能的,你可能想使用的malloc()在需要显式转换的背景。我能想到的唯一情况是路过的结果的的malloc()直接将需要比其他无效* 。在这样的指针类型的参数的可变参数函数一个不寻常的和人为的情况下,铸件可以通过分配的结果的指针对象并传递该对象的值的函数来避免。)

(It's barely conceivable that you might want to use malloc() in a context where an explicit conversion is needed. The only case I can think of is passing the result of malloc() directly to a variadic function that needs an argument of a pointer type other than void*. In such an unusual and contrived case, the cast can be avoided by assigning the result to a pointer object and passing that object's value to the function.)

唯一的好理由来投的结果的malloc()是(a)与C兼容性++(但需要写code的编译既为C和C ++罕见的比你想象的),和(b)兼容pre-ANSI C编译器(但需要满足这样的编译器是罕见的越来越少了)。

The only good reasons to cast the result of malloc() are (a) compatibility with C++ (but the need to write code that compiles both as C and as C++ rarer than you might expect), and (b) compatibility with pre-ANSI C compilers (but the need to cater to such compilers is rare and becoming rarer).

这篇关于当我需要在C语言中投malloc的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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