为什么铸造malloc是一件坏事? [英] why is casting malloc a bad thing?

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

问题描述

你好,


我在最近的一些帖子中看到人们说回购

值的malloc很糟糕,如:


d =(double *)malloc(50 * sizeof(double));


为什么这么糟糕?我一直认为(可能是错误的)无效指针的目的是强制转换为合法的日期类型。

这个错了吗?为什么,以及什么被认为是正确的形式?


谢谢,


Brian Blais

Hello,

I saw on a couple of recent posts people saying that casting the return
value of malloc is bad, like:

d=(double *) malloc(50*sizeof(double));

why is this bad? I had always thought (perhaps mistakenly) that the
purpose of a void pointer was to cast into a legitimate date type. Is
this wrong? Why, and what is considered to be correct form?

thanks,

Brian Blais

推荐答案

Brian Blais< bb **** @ bryant.edu>潦草地写了下面的内容:
Brian Blais <bb****@bryant.edu> scribbled the following:
你好,
我在最近的一些帖子中看到人们说,施放malloc的返回值很糟糕,如:
d =( double *)malloc(50 * sizeof(double));
为什么这么糟糕?我一直认为(可能是错误地)void指针的目的是强制转换为合法的日期类型。这是错的吗?为什么,以及什么被认为是正确的形式?
Hello, I saw on a couple of recent posts people saying that casting the return
value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought (perhaps mistakenly) that the
purpose of a void pointer was to cast into a legitimate date type. Is
this wrong? Why, and what is considered to be correct form?




这不完全_wrong_但也没有任何帮助。为什么人们会因为这些原因而不鼓励这个因素:

1)没有必要。 malloc(),正确的原型,返回一个

void *,它可以分配给任何对象指针类型。

2)如果malloc()没有正确原型,那么返回值

不会解决任何问题。 malloc()仍将返回int,并将

int转换为对象指针类型并不会奇怪地将其更改为

指针。

3)如果malloc()没有正确原型化,代码就会被破坏,因为它会导致未定义的行为。然而,施放malloc()会使得编译器_think_它没有被破坏,虽然确实如此。

正确的形式很简单:

d = malloc(50 * sizeof(double));

或:

d = malloc(50 * sizeof * d);


-

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

\ - http: //www.helsinki.fi/~palaste ---------------------规则! -------- /

" Hasta la Vista,Abie! 

- Bart Simpson



It''s not exactly _wrong_ but doesn''t help anything either. Why people
discourage it is because of these reasons:
1) There is no need to. malloc(), correctly prototyped, returns a
void *, which is assignable to any object pointer type anyway.
2) If malloc() is not correctly prototyped, casting the return value
won''t fix anything. malloc() will still return int, and casting that
int to an object pointer type isn''t magically going to change it to a
pointer.
3) If malloc() is not correctly prototyped, the code is broken, because
it causes undefined behaviour. However casting the malloc() will make
the compiler _think_ it''s not broken, although it really is.
The correct form is simply:
d = malloc(50*sizeof(double));
or:
d = malloc(50*sizeof *d);

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Hasta la Vista, Abie!"
- Bart Simpson


Brian Blais< bb **** @ bryant.edu>在新闻中写道:40 ************ @ bryant.edu:
Brian Blais <bb****@bryant.edu> wrote in news:40************@bryant.edu:
你好,

我在几个最近的帖子有人说,施放malloc的返回值很差,比如:

d =(double *)malloc(50 * sizeof(double));


请参阅C-FAQ了解原因。
http://www.eskimo.com/~scs/C-faq/q7.6.html
http://www.eskimo.com/~scs/ C-faq / q7.7.html

为什么这么糟糕?我一直认为(可能是错误的)无效指针的目的是强制转换为合法的日期类型。
Hello,

I saw on a couple of recent posts people saying that casting the return
value of malloc is bad, like:

d=(double *) malloc(50*sizeof(double));
See the C-FAQ as to why.

http://www.eskimo.com/~scs/C-faq/q7.6.html
http://www.eskimo.com/~scs/C-faq/q7.7.html
why is this bad? I had always thought (perhaps mistakenly) that the
purpose of a void pointer was to cast into a legitimate date type.




否定。无效指针可以容纳任何类型的指针,因此它们自然地会变成变成指针。所需的类型。


例如


void foo(void * pThing)

{

int idx;

char * pChar = pThing;


/ *使用pChar * /

for(idx = 0; idx< 10; ++ idx)

{

pChar [idx] =''a'';

} < br $>
}


无效栏(无效)

{

int arr [100];


foo(arr);

}


不需要或不需要任何icky演员。 Void是你的朋友。


-

- 马克 - >

-



Negative. Void pointers can hold any type of pointer so they naturally
"become" the desired type.

E.g.

void foo(void *pThing)
{
int idx;
char *pChar = pThing;

/* work with pChar */
for (idx = 0; idx < 10; ++idx)
{
pChar[idx] = ''a'';
}
}

void bar(void)
{
int arr[100];

foo(arr);
}

No icky casts needed or desired. Void is your friend.

--
- Mark ->
--


Brian Blais写道:
Brian Blais wrote:
你好,

我在最近几篇帖子中看到人们说要投出malloc的返回价值很糟糕,比如:

d =(double *)malloc(50 * sizeof(double));

为什么这么糟糕?我一直认为(可能是错误地)void指针的目的是强制转换为合法的日期类型。这是错的吗?为什么,以及什么被认为是正确的形式?
Hello,

I saw on a couple of recent posts people saying that casting the return
value of malloc is bad, like:

d=(double *) malloc(50*sizeof(double));

why is this bad? I had always thought (perhaps mistakenly) that the
purpose of a void pointer was to cast into a legitimate date type. Is
this wrong? Why, and what is considered to be correct form?




指向void(void *)的指针可以*隐式*转换为

指向任何对象类型的指针,使得转换不必要。很可能是因为在ANSI(简称K& R)C之前的情况不是这样的事实而引起的混淆,其中显式演员*是必要的。


上面调用malloc()的首选是:


d = malloc(50 * sizeof * d );


这有几个优点:


它更短。 ;-)


如果一个人未能#include< stdlib.h> (在这种情况下,返回类型

malloc()恢复为隐式`int'')将生成错误。这是

在int和void

*的大小不同的平台上特别重要;在这种情况下,显式转换将允许代码编译,但行为奇怪..


如果d的类型发生变化,表格malloc()调用没有改变




一般来说,施法是邪恶的,除非它是必要的。既然在这种情况下不需要
,为什么呢?

这个问题在新闻中反复出现:comp.lang.c。如果您有进一步的问题,我建议您搜索档案(例如,通过

http://groups.google.com)


HTH,

- ag

-

Artie Gold - 德克萨斯州奥斯汀



A pointer to void (void *) can be *implicitly* converted from and to a
pointer to any object type, making the cast unnecessary. It is likely
that the confusion arises from the fact that this was not the case in
pre-ANSI (referred to as K&R) C, where the explicit cast *was* necessary.

The preferred for of the call to malloc() above is:

d = malloc(50 * sizeof *d);

This has several advantages:

It''s shorter. ;-)

If one fails to #include <stdlib.h> (in which case the return type of
malloc() reverts to implicit `int'') an error will be generated. This is
particularly significant on platforms where the size of `int'' and `void
*'' differ; in such cases the explicit cast would allow the code to
compile, but behave strangely..

If the type of `d'' changes, the form of the malloc() call does not have
to be changed.

In general, casting is evil, except where it''s necessary. Since it isn''t
necessary in this case, why do it?
This issue has appeared repeatedly on news:comp.lang.c. If you have
further questions, I would recommend searching the archives (through,
for example, http://groups.google.com).

HTH,
--ag
--
Artie Gold -- Austin, Texas


这篇关于为什么铸造malloc是一件坏事?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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