为什么我们不能隐式地从一个void *分配在C任何其他类型++但其$ P $用C pferred? [英] why we cannot implicitly assign from a void* to any other type in C++ but its preferred in C?

查看:215
本文介绍了为什么我们不能隐式地从一个void *分配在C任何其他类型++但其$ P $用C pferred?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据<一个href=\"http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?id=1043284351&answer=1047673478\">this

这是preferred在不投的malloc C ,因为如果返回的的malloc 是投那么这将是错误被标记的被隐藏,造成了难以发现错误。另外,在维护期间,如果该指针的变化,但铸类型未改变,再一次有一个难以发现错误。最有经验的程序员选择的方法是:

  P =的malloc(N * sizeof的* P);

有没有投对于的malloc ,因为没有必要为一体,和而不是使用的的sizeof(类型),以确定该块的大小, * sizeof的使用PTR 。通过取消引用指针,并采取它的大小,适当的值,而不必担心修改分配请求给定的,如果该类型的指针的变化。

,但它不会用C ++编译。为什么?

?它意味着,如果返回的的malloc 是投那么这将被标记错误是隐藏的?


解决方案

  

,但它不会用C ++编译。为什么?


这是使用的便利性之间进行权衡无效* 作为一种通用的指针,preventing无效类型转换的安全性。

在C,无效* 被广泛使用,因为它是编写普通code的唯一合理的方法;这样,在该语言,它可以被隐式转换为另一种类型的指针为方便起见

在C ++中,有编写通用code的类型安全的方式,让无效* 很少使用;特别是,你使用类型安全的来分配和初始化的对象,而不是的malloc 分配原始内存和pretend它包含了一个对象。所以,在那种语言,从 void *的隐式转换禁止给予更强的类型安全。


  

?它意味着,如果的malloc返回是投那么这将被标记错误是隐藏的?


线索是在previous一句:


  

有什么不对的,除了在事件文件stdlib.h ,其中声明的malloc 的头,不包括


在(旧方言)C,你可以调用它并没有被宣布为功能;隐式声明了一个返回类型 INT 的功能。所以,如果你要调用的malloc 不包括头,你会得到code,它错误地认为它返回 INT 。如果没有一个演员,你会当你试图将其分配到一个指针得到一个编译错误。随着投,code编译会,这可能使模糊的运行时错误和漫长的调试会话。

在C ++(和现代的C),所有功能都必须在使用之前声明,所以即使你没有使用的malloc 由于某种原因,不可能发生这种错误。

According to this

It is preferred to not cast malloc in C because if the return of malloc is cast then the error which would be flagged is hidden, resulting in a difficult to find bug. Also, during maintenance, if the type of the pointer changes but the cast is not changed, once again there is a difficult to find bug. The method most experienced programmers choose is:

p = malloc ( n * sizeof *p );

There is no cast for malloc since there is no need for one, and instead of using sizeof ( type ) to determine the size of the block, sizeof *ptr is used. By dereferencing the pointer and taking its size, the proper value is given without having to worry about modifying the allocation request if the type of the pointer changes.

but it won't compile in C++. why?

and what does it means that if the return of malloc is cast then the error which would be flagged is hidden ?

解决方案

but it won't compile in C++. why?

It's a trade-off between the convenience of using void* as a generic pointer, and the safety of preventing invalid type conversions.

In C, void* is widely used, as it's the only reasonable way to write generic code; so, in that language, it can be implicitly converted to another pointer type for convenience.

In C++, there are type-safe ways of writing generic code, so void* is rarely used; in particular, you'd use the type-safe new to allocate and initialise an object, rather than malloc to allocate raw memory and pretend it contained an object. So, in that language, implicit conversion from void* is forbidden to give stronger type safety.

and what does it means that if the return of malloc is cast then the error which would be flagged is hidden ?

The clue is in the previous sentence:

There is nothing wrong with this except in the event that stdlib.h, the header which declares malloc, is not included

In (older dialects of) C, you can call a function which hasn't been declared; that implicitly declares a function with a return type of int. So, if you were to call malloc without including the header, you'd get code that erroneously assumed that it returned int. Without a cast, you'd get a compiler error when you tried to assign it to a pointer. With a cast, the code would compile, potentially giving obscure runtime errors and lengthy debugging sessions.

In C++ (and modern C), all functions must be declared before use, so this error couldn't happen even if you did use malloc for some reason.

这篇关于为什么我们不能隐式地从一个void *分配在C任何其他类型++但其$ P $用C pferred?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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