铸造返回malloc [英] Casting return of malloc

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

问题描述



我读过一篇旧帖子,里面没有指针,因为malloc会返回
。我想知道原因。

提前致谢,

YTR

解决方案

在文章< 11 ********************** @ g14g2000cwa.googlegroups .com>,
yt **** @ gmail.com < yt **** @ gmail.com>写道:


我已经阅读了一篇旧帖子,其中没有指出由malloc返回的指针。我想知道原因。




没有必要,它隐藏了错误。为什么额外输入

带有负面利益的东西?

dave


-

Dave Vandervies dj******@csclub.uwaterloo.ca

话虽如此,你的错误往往比大多数人少得多(

可能就是为什么我们采取这种不圣洁的喜悦来试图找到错误的

你是什么意思写)。 --Richard Heathfield in comp.lang.c


yt **** @ gmail .com < yt **** @ gmail.com>潦草地写了下面的内容:


我已经阅读了一篇旧帖子,其中没有指出由malloc返回的指针。我想知道原因。




它不会修复任何东西,但它可能会让编译器认为问题是

已修复当他们真的不是。


更长的解释:

没有适当的malloc()原型,编译器认为它返回

int。因此,当malloc创建void *值并返回它时,

编译器会隐式将其转换为int。在这种情况下,值已经*已经*分解为

,因此任何投射到任何东西的数量都无济于事。但是,如果你将它转换为指针类型,那么

,编译器会认为*你知道你正在做什么,并且省略警告。因此你会得到破碎的代码

看起来像工作代码。


-

/ - Joona Palaste(pa ** ***@cc.helsinki.fi)-------------芬兰-------- \

\ ------ --------------------------------------------------规则! -------- /

我们的巫师不喜欢吃我们的话,所以说。

- Sparrowhawk


" YT **** @ gmail.com" < YT **** @ gmail.com>写道:

我读过一篇旧帖子,里面没有指出由malloc返回的指针。我想知道原因。




我不建议投出malloc()的返回值:


* ANSI C中不要求强制转换。


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

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


*如果你偶然输入了错误的类型,奇怪的失败可能会导致
结果。


其他一些人不同意,例如PJ Plauger(见文章

< 9s ***************** @nwrddc01.gnilink.net> ;)。


-

"这是一个很棒的答案。

它是偏离主题的,它这是不正确的,它没有回答这个问题。

- 理查德希思菲尔德


Hi,
I have read in one of old posting that don''t cast of pointer which
is returned by the malloc. I would like to know the reason.

Thanks in advance,
YTR

解决方案

In article <11**********************@g14g2000cwa.googlegroups .com>,
yt****@gmail.com <yt****@gmail.com> wrote:

Hi,
I have read in one of old posting that don''t cast of pointer which
is returned by the malloc. I would like to know the reason.



It isn''t necessary, and it hides errors. Why do the extra typing for
something with a negative benefit?
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca
Having said that, you tend to be a lot less wrong than most people (which
might be why we take such an unholy delight in trying to find errors in
what you write). --Richard Heathfield in comp.lang.c


yt****@gmail.com <yt****@gmail.com> scribbled the following:

Hi,
I have read in one of old posting that don''t cast of pointer which
is returned by the malloc. I would like to know the reason.



It won''t fix anything, but it may make the compiler think problems are
fixed when they really aren''t.

Longer explanation:
Without a proper prototype for malloc(), the compiler thinks it returns
int. Thus, when malloc creates a void* value and returns it, the
compiler implicitly converts it to int. The value is *already* broken at
this case, so no amount of casting it to anything will help. However,
if you cast it to a pointer type, the compiler will *think* you know
what you''re doing, and omit a warning. Thus you get broken code that
looks like working code.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"We sorcerers don''t like to eat our words, so to say."
- Sparrowhawk


"yt****@gmail.com" <yt****@gmail.com> writes:

I have read in one of old posting that don''t cast of pointer which
is returned by the malloc. I would like to know the reason.



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.

Some others do disagree, such as P.J. Plauger (see article
<9s*****************@nwrddc01.gnilink.net>).

--
"This is a wonderful answer.
It''s off-topic, it''s incorrect, and it doesn''t answer the question."
--Richard Heathfield


这篇关于铸造返回malloc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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