(FAQ详情:) malloc(),void *和强制转换 [英] (FAQ details:) malloc(), void * and casts

查看:74
本文介绍了(FAQ详情:) malloc(),void *和强制转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



本主题是常见问题解答。但我已经阅读了常见问题,花了几个小时浏览群组档案,还有几个问题,我希望你能回答这个问题。


我的理解是推荐的做法是不从malloc()转换

返回值。这个的基本原理是:1)不需要

演员阵容2)演员阵容可能会掩盖错误。


我认为演员阵容的原因是不需要与

事实有关,即从malloc()返回的指针是一个void *,而不是

a指向任何其他类型的指针。 (这是正确的吗?)


如果是这样,你能不能解释_why_(及其原因的细节)施放

无效指针是不是必要的? />
(对于我天真的眼睛,将一种类型的指针(void)分配给指针
另一种类型的
(例如int)似乎并不完全正确。)


我的桌子上有规格(ISO 9899:1999),但我不熟悉

足以找到这个答案。所以参考

规范,可能还有一些解释,也会有所帮助。

亲切的问候

Asbj ?? sn? | b ??

解决方案

Asbj ?? rn S?| b ??写道:


我的理解是建议的做法是不从malloc()转换

返回值。这个的基本原理是:1)不需要

施放,2)施法可能掩盖错误。



就是这样。


我认为不需要演员表的原因与

这一事实,从malloc()返回的指针是一个void *,而不是

a指向任何其他类型的指针。 (这是正确的吗?)



准确地说。


如果是这样,你能解释一下_why_(和详情为什么)施放

无效指针是不是必要?



因为根据标准,指向void的指针可以转换为

和任何其他对象指针类型。
< blockquote class =post_quotes>
(对于我天真的眼睛,将一种类型的指针(void)分配给一个指针
另一种类型的
(例如int)似乎并不完全正确的。



它是(标准)用于标准的无效*的部分内容,因为我知道
了解它。


我的桌面上有规格(ISO 9899:1999),但我不熟悉

足够它找到这个答案。所以对

规范的引用,可能还有一些解释,也会有所帮助。



我认为第6.3.2.3节相当清楚。


Mark Bluemel< ma *** *******@pobox.comwrites:


Asbj ?? rn S?| b ??写道:


[为什么没有必要从malloc()转换返回值]


如果是这样,你能不能解释_why_(及其原因的细节)施放

无效指针?



因为根据标准,指向void的指针可以转换为

和任何其他对象指针类型。

[...]


我认为第6.3.2.3节相当清楚。



"指向void的指针可以转换为指向任何

不完整或对象类型的指针。 [...]


这种转换是隐含的吗?并且它是犹太洁食。在各方面,并且

不应该从编译器中引出任何警告(诊断?)?


我问的原因是我曾经告诉我们至少在工作中使用的Lint会反对为例如指定一个空*一个int *。


Asbj ?? rn


Asbj ?? rn S?| b ?? < in ***** @ invalid.invalidwrites:


本主题是常见问题解答。但我已经阅读了常见问题,花了几个小时浏览群组档案,还有几个问题,我希望你能回答这个问题。


我的理解是推荐的做法是不从malloc()转换

返回值。这个的基本原理是:1)不需要

演员阵容2)演员阵容可能会掩盖错误。


我认为演员阵容的原因是不需要与

事实有关,即从malloc()返回的指针是一个void *,而不是

a指向任何其他类型的指针。 (这是正确的吗?)


如果是这样,你能不能解释_why_(及其原因的细节)施放

无效指针是不是必要的? />
(对于我天真的眼睛,将一种类型的指针(void)分配给一个指针
另一种类型的
(例如int)似乎并不完全正确。)



不是。因此C语言使用

隐式转换为您进行转换。 (我认为那是术语......)

http://www.stanford.edu/~blp/writing...lloc-cast.html
http://www.cpax.org.uk/prg/writings/casting.php


>

我的桌面上有规格(ISO 9899:1999),但我不熟悉

足以找到这个答案。所以参考

规范,可能还有一些解释,也会有所帮助。


亲切的问候

Asbj? ?S?| b ??



This topic is a FAQ. But I have read the faq and spent a couple of
hours browsing the group archives, and still have a few questions that
I hope you can answer.

My understanding is that recommended practice is to not cast the
return value from malloc(). The rationale for this is that 1) the
cast is not needed and 2) the cast may mask errors.

I assume that the reason the cast is not needed has to do with the
fact that the the pointer returned from malloc() is a void *, and not
a pointer to any other type. (Is that correct?)

If so, could you explain _why_ (and the details of why) casting the
void pointer is not necessary?
(For my naive eye, assigning a pointer of one type (void) to a pointer
of another type (e.g. int) does not seem quite "correct".)

I do have the spec (ISO 9899:1999) at my desk, but I am not familiar
enough with it to find the answer to this one. So references to the
spec, possibly along with some interpretation, would also be helpful.
With kind regards
Asbj??rn S?|b??

解决方案

Asbj??rn S?|b?? wrote:

My understanding is that recommended practice is to not cast the
return value from malloc(). The rationale for this is that 1) the
cast is not needed and 2) the cast may mask errors.

That is so.

I assume that the reason the cast is not needed has to do with the
fact that the the pointer returned from malloc() is a void *, and not
a pointer to any other type. (Is that correct?)

Precisely.

If so, could you explain _why_ (and the details of why) casting the
void pointer is not necessary?

Because according to the standard, pointer to void can be converted to
and from any other object pointer type.

(For my naive eye, assigning a pointer of one type (void) to a pointer
of another type (e.g. int) does not seem quite "correct".)

It''s (part of) what the standard intended void * to be used for, as I
understand it.

I do have the spec (ISO 9899:1999) at my desk, but I am not familiar
enough with it to find the answer to this one. So references to the
spec, possibly along with some interpretation, would also be helpful.

Section 6.3.2.3 is fairly clear, I think.


Mark Bluemel <ma**********@pobox.comwrites:

Asbj??rn S?|b?? wrote:

[Why it it not necessary to cast the return value from malloc() ]

If so, could you explain _why_ (and the details of why) casting the
void pointer is not necessary?


Because according to the standard, pointer to void can be converted to
and from any other object pointer type.
[...]

Section 6.3.2.3 is fairly clear, I think.

"A pointer to void may be converted to or from a pointer to any
incomplete or object type. [...]"

And this conversion is implicit? And it is "kosher" in every way, and
should not elicit any warnings ("diagnostics"?) from the compiler?

The reason I ask is that I have been told that at least the Lint we
use at work will object to assigning a void* to e.g. an int *.

Asbj??rn


Asbj??rn S?|b?? <in*****@invalid.invalidwrites:

This topic is a FAQ. But I have read the faq and spent a couple of
hours browsing the group archives, and still have a few questions that
I hope you can answer.

My understanding is that recommended practice is to not cast the
return value from malloc(). The rationale for this is that 1) the
cast is not needed and 2) the cast may mask errors.

I assume that the reason the cast is not needed has to do with the
fact that the the pointer returned from malloc() is a void *, and not
a pointer to any other type. (Is that correct?)

If so, could you explain _why_ (and the details of why) casting the
void pointer is not necessary?
(For my naive eye, assigning a pointer of one type (void) to a pointer
of another type (e.g. int) does not seem quite "correct".)

It isn''t. And so the C language does the conversion for you using an
implicit conversion. (I think thats the terminology...)

http://www.stanford.edu/~blp/writing...lloc-cast.html
http://www.cpax.org.uk/prg/writings/casting.php

>
I do have the spec (ISO 9899:1999) at my desk, but I am not familiar
enough with it to find the answer to this one. So references to the
spec, possibly along with some interpretation, would also be helpful.
With kind regards
Asbj??rn S?|b??


这篇关于(FAQ详情:) malloc(),void *和强制转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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