malloc和指针转换 [英] malloc and pointer casting

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

问题描述

你好,我是这个名单的新手,一般来说是Usenet,

所以请原谅(和建议)我,如果我做错了什么。


无论如何。


我有点困惑,因为我总是认为一个

_should_显式地转换了malloc返回的void *指针。 />
现在我读过帖子说这很危险。


有人可以清楚这个吗?


谢谢

Mirko

Hello, I''m new to this list and to Usenet in general,
so please forgive (and advice) me, if I do something wrong.

Anyway.

I am a bit confused, because I always thought one
_should_ explicitly cast the void* pointer returned by malloc.
Now I read postings saying that this is dangerous.

Could somebody please clearify this?

Thanks
Mirko

推荐答案

Mirko在31/08/05写道:
Mirko wrote on 31/08/05 :
你好,我是这个列表和一般的Usenet的新手,
所以请原谅(和建议)我,如果我做错了什么。

我有点困惑,因为我总是想一个
_should_显式地转换了malloc返回的void *指针。
Hello, I''m new to this list and to Usenet in general,
so please forgive (and advice) me, if I do something wrong.

I am a bit confused, because I always thought one
_should_ explicitly cast the void* pointer returned by malloc.




你可以,但这只是浪费时间。请阅读常见问题解答。


-

Emmanuel

C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

C库: http:// www .dinkumware.com / refxc.html


我曾经问过一位专家COBOL程序员,如何用
在COBOL中声明局部变量,回复是:

什么是局部变量?



You can, but it''s just another waste of time. Please read the FAQ.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

I once asked an expert COBOL programmer, how to
declare local variables in COBOL, the reply was:
"what is a local variable?"




" Mirko" < MK **** @ gmx.net>在消息中写道

news:11 ********************** @ f14g2000cwb.googlegr oups.com ...

"Mirko" <mk****@gmx.net> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
你好,我是这个名单的新手,一般来说是Usenet,
所以请原谅(和建议)我,如果我做错了什么。

无论如何。

我有点困惑,因为我一直认为一个
_should_显式地转换了malloc返回的void *指针。


你为什么这么认为?如果你是从一本书中学到的,那么请告诉我们标题和作者,所以我们可以警告

其他人。

现在我读了帖子说这很危险。

有人可以清楚这个吗?
Hello, I''m new to this list and to Usenet in general,
so please forgive (and advice) me, if I do something wrong.

Anyway.

I am a bit confused, because I always thought one
_should_ explicitly cast the void* pointer returned by malloc.
Why did you think that? If you learned it from a book,
please tell us the title and author so we can warn
others about it.
Now I read postings saying that this is dangerous.

Could somebody please clearify this?




从''malloc()''转换返回值可以掩码错误

(例如,未通过#include

< stdlib.h>提供原型)。在C89中,调用没有

原型的函数将导致编译器假设其返回

类型为''int''。因此,返回的指针将被视为类型为''int''的
,通常会破坏其值。

C99使这个问题没有实际意义,因为它需要原型

可以在任何调用函数的范围内。但是C99还没有广泛使用,所以这仍然是一个非常实际的问题。


A'' void *''指针可以隐式转换为任何

其他指针类型,无需任何演员。


IOW做的事情如下:


#include< stdlib.h>


int main()

{

int * p = malloc(sizeof * p * 100); / * 100是任意的

这个例子的数字* /


if(p)

{

/ *用'p'做点什么* /


免费(p);

}

else

/ *内存分配失败,采取纠正措施* /

返回0;

}


请注意,我还解决了''malloc()''的另一个常见错误处理:

未能检查其返回值是否成功。

comp.lang.c常见问题:
http://www.eskimo.com/~scs/C-faq/faq.html

-Mike



Casting the return value from ''malloc()'' can mask errors
(e.g. failure to provide its prototype via #including
<stdlib.h>). In C89, calling a function without a
prototype will cause a compiler to assume its return
type is ''int''. So the returned pointer would be
treated as type ''int'', often corrupting its value.
C99 makes this issue moot since it requires a prototype
be in scope for any function called. However C99 is
not in very widespread use yet, so this is still a
very real issue.

A ''void*'' pointer can be implicitly converted to any
other pointer type without any cast.

IOW do things like so:

#include <stdlib.h>

int main()
{
int *p = malloc(sizeof *p * 100); /* 100 is arbitrary
number for this example */

if(p)
{
/* do something with ''p'' */

free(p);
}
else
/* memory allocation failed, take corrective action */

return 0;
}

Note that I also addressed another common mishandling of ''malloc()'':
failure to check its return value to see if it succeeded or not.
comp.lang.c Frequently Asked Questions:
http://www.eskimo.com/~scs/C-faq/faq.html

-Mike

Mike Wahler写道:
Mike Wahler wrote:

Mirko < MK **** @ gmx.net>在消息中写道

"Mirko" <mk****@gmx.net> wrote in message



[...从malloc()转换返回...]


[... casting the return from malloc() ...]

现在我读了帖子说这很危险。

有人可以清楚这个吗?
Now I read postings saying that this is dangerous.

Could somebody please clearify this?



从''malloc()''转换返回值可以掩盖错误(例如,未通过#including
< stdlib.h>提供原型)。在C89中,调用没有原型的函数将导致编译器假设其返回类型为''int''。所以返回的指针将被视为类型''int'',经常破坏它的值。



Casting the return value from ''malloc()'' can mask errors
(e.g. failure to provide its prototype via #including
<stdlib.h>). In C89, calling a function without a
prototype will cause a compiler to assume its return
type is ''int''. So the returned pointer would be
treated as type ''int'', often corrupting its value.



[...]


澄清这一点...


在某些系统上,sizeof(int)< sizeof(void *),意思是,最多只有
返回值的高位丢失,导致无效的
指针。 (例如,具有32位FAR指针的16位DOS系统。)


更糟糕的是,某些系统返回的指针与整数不同。对于

示例,我看到一个带有两组寄存器的CPU - 一组用于数据,

一组用于地址 - 并且存储了函数的返回值

在相应的寄存器中。所以,malloc()返回

A0寄存器中的指针,而你的代码认为malloc()返回一个int,

读取D0寄存器的值。


从malloc()转换回来会隐藏任何警告。

-

+ ------- ------------------ + -------------------- + ---------- ------------------- +

| Kenneth J. Brody | www.hvcomputer.com | |

| kenbrody / at\spamcop.net | www.fptech.com | #include< std_disclaimer.h> |

+ ------------------------- + -------------- ------ + ----------------------------- +

不要给我发电子邮件:< mailto:Th ************* @ gmail.com>


[...]

To clarify this point...

On some systems, sizeof(int) < sizeof(void*), meaning that, at best,
the upper bits of the return value are lost, resulting in an invalid
pointer. (For example, a 16-bit DOS system with 32-bit FAR pointers.)

Worse, some systems return pointers differently than integers. For
example, I''ve seen a CPU with two sets of registers -- one for data,
one for addresses -- and the return value from functions was stored
in the appropriate register. So, malloc() returns the pointer in the
A0 register, while your code, thinking that malloc() returns an int,
reads the D0 register for the value.

Casting the return from malloc() would hide any warnings.
--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don''t e-mail me at: <mailto:Th*************@gmail.com>


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

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