由malloc()返回的void指针的转换 [英] Casting of void pointer returned by malloc()

查看:190
本文介绍了由malloc()返回的void指针的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么不建议通过malloc()明确地对返回的void指针进行类型转换

;功能?


例如:

{

int * p;

p =(int * )的malloc(2 *的sizeof(int)的); / *显式铸造完成,因此

据说这是一种不好的做法* /

/ *有些代码* /

}


请解释原因。

提前致谢。

Why is it discouraged to explicitly typecast the void pointer returned
by malloc(); function?

For example:
{
int *p;
p = (int*)malloc(2*sizeof(int)); /*Explicit casting is done, therfore
it is said to be a bad practice*/
/*Some codes*/
}

Please, explain why.

Thanks in advance.

推荐答案

3月6日16:18,SRR < SRRajesh1 ... @ gmail.comwrote:
On 6 Mar, 16:18, "SRR" <SRRajesh1...@gmail.comwrote:

为什么不建议明确地对返回的void指针进行类型转换

by malloc() ;功能?


例如:

{

int * p;

p =(int * )的malloc(2 *的sizeof(int)的); / *显式铸造完成,因此

据说这是一种不好的做法* /

/ *有些代码* /


}


请解释原因。
Why is it discouraged to explicitly typecast the void pointer returned
by malloc(); function?

For example:
{
int *p;
p = (int*)malloc(2*sizeof(int)); /*Explicit casting is done, therfore
it is said to be a bad practice*/
/*Some codes*/

}

Please, explain why.



FAQ 7.7b< http://c-faq.com/malloc/mallocnocast.html>


I建议你给FAQ添加书签并在提问之前检查它。


注意:FAQ 7.7b中讨论的问题是真的,我看过

它导致指针为64位的系统出现重大问题,但

整数为32 ......

FAQ 7.7b <http://c-faq.com/malloc/mallocnocast.html>

I suggest you bookmark the FAQ and check it before asking questions.

NOTE: The problem discussed in FAQ 7.7b is a real one, and I''ve seen
it causing major problems on systems where a pointer is 64-bits, but
an integer 32...


SRR写道:
SRR wrote:

为什么不鼓励明确地强制转换
Why is it discouraged to explicitly typecast



" cast"。不是类型转换。

"cast". Not "typecast".


malloc()返回的void指针;功能?
the void pointer returned by malloc(); function?



这是在FAQ中。


(因为你不需要,这样做可以隐瞒了一个严重的错误,编译器没有/有义务/

报告。)


-

避免刺猬

根据他们迄今为止的行为 - 我不知道 / Sahara /

It''s in the FAQ.

(Because you don''t need to, and doing so can conceal a
serious mistake that the compiler is not /obliged/
to report.)

--
Avoid Hedgehog
"Based on their behaviour so far -- I have no idea" /Sahara/


" SRR" < SR ********** @ gmail.comwrites:
"SRR" <SR**********@gmail.comwrites:

为什么不鼓励显式地对返回的void指针进行类型转换

by malloc();功能?
Why is it discouraged to explicitly typecast the void pointer returned
by malloc(); function?



我不建议转换malloc()的返回值:


*演员表不是必需的ANSI C.


*转换它的返回值可以掩盖#include

< stdlib.h>的失败,这会导致未定义的行为。


*如果你偶然输入了错误的类型,奇怪的失败可以

结果。


在特殊情况下它可能有意义来转换

malloc()的返回值。例如,PJ Plauger有充分的理由想要将他的

代码编译为C和C ++,并且C ++需要演员,因为他在文章中解释了&b
。 9s******************@nwrddc01.gnilink.net>。

然而,Plauger的情况确实很少见。大多数程序员应该将他们的代码编写为C或C ++,而不是两者的交集。


-

......我犯了什么愚蠢,我奉献给你。

- 威廉·莎士比亚,_Troilus和Cressida_

I don''t recommend casting the return value of malloc():

* The cast is not required in ANSI C.

* Casting its return value can mask a failure to #include
<stdlib.h>, which leads to undefined behavior.

* If you cast to the wrong type by accident, odd failures can
result.

In unusual circumstances it may make sense to cast the return value of
malloc(). P. J. Plauger, for example, has good reasons to want his
code to compile as both C and C++, and C++ requires the cast, as he
explained in article <9s*****************@nwrddc01.gnilink.net>.
However, Plauger''s case is rare indeed. Most programmers should write
their code as either C or C++, not in the intersection of the two.

--
"...what folly I commit, I dedicate to you."
--William Shakespeare, _Troilus and Cressida_


这篇关于由malloc()返回的void指针的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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