指针从'void *'截断到'int' [英] pointer truncation from 'void *' to 'int'

查看:364
本文介绍了指针从'void *'截断到'int'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我收到编译错误指针截断从''void *''到

''int' "因为我没有明确地从void *转换为(datatype *)。

指针存储为整数(是吗?),所以作为AFAIK,有

没有什么可担心的吗?


我正在使用VC 7.1


为什么这些作业被视为错误?

Hi,

I''m getting a compiler error of "pointer truncation from ''void *'' to
''int''" because I''m not explicitly casting from void* to (datatype*).
Pointers are stored as ints (is that right?), so as AFAIK, there is
nothing to worry about is there?

I''m using VC 7.1

Why are such assignments being treated as errors?

推荐答案

>

我收到的编译错误是指针截断从''void *''到
''int''"因为我没有明确地从void *转换为(datatype *)。
指针存储为int(是吗?),所以作为AFAIK,有什么可担心的吗?

我正在使用VC 7.1

为什么这样的作业被视为错误?

I''m getting a compiler error of "pointer truncation from ''void *'' to
''int''" because I''m not explicitly casting from void* to (datatype*).
Pointers are stored as ints (is that right?), so as AFAIK, there is
nothing to worry about is there?

I''m using VC 7.1

Why are such assignments being treated as errors?




你是担心将void *转换为int是否确实涉及

截断?我是C的新手,但似乎sizeof会为你回答这个

的问题。我想,指针所需的空间将从一台机器到另一台机器不同,具体取决于可用内存的总量,而int的大小更可能只是匹配

IEEE推荐。



You are worried about whether converting void* to int really involves
truncation? I am new to C, but it seems that sizeof would answer this
question for you. I imagine that the space required for pointers would
vary from one machine to another, depending on the total amount of
available memory, while the size of int would more likely just match
the IEEE recommendation.


" Alfonso Morra" < SW *********** @ the-ring.com>在消息中写道

news:dc ********** @ nwrdmz02.dmz.ncs.ea.ibs-infra.bt.com ...
"Alfonso Morra" <sw***********@the-ring.com> wrote in message
news:dc**********@nwrdmz02.dmz.ncs.ea.ibs-infra.bt.com...


我得到的编译错误是指针截断从''void *''到
''int''因为我没有明确地从void *转换为(datatype *)。
指针存储为int(是吗?),所以作为AFAIK,有什么可担心的吗?

我正在使用VC 7.1

为什么这样的作业被视为错误?
Hi,

I''m getting a compiler error of "pointer truncation from ''void *'' to
''int''" because I''m not explicitly casting from void* to (datatype*).
Pointers are stored as ints (is that right?), so as AFAIK, there is
nothing to worry about is there?

I''m using VC 7.1

Why are such assignments being treated as errors?




因为sizeof (void *)并不一定等于sizeof(int)或

sizeof(void(*)()),它是所有平台相关的(有时甚至是编译器

依赖)。并且,虽然指针是存储单元的整数地址,但是从语义上看它是一个与整数非常不同的类型,所以你不应该期望编译器吃掉你的代码并没有发牢骚。


Alex



Because sizeof(void*) doesn''t necessarily equal to sizeof(int) nor
sizeof(void(*)()), it''s all platform dependent (and sometimes even compiler
dependent). And, although a pointer is an integer address of a memory cell,
semantically its a very different type from an integer, so you shouldn''t
expect the compiler eat your code and doesn''t eructate.

Alex


Alfonso Morra写道:
Alfonso Morra wrote:
你好,

我得到的编译错误是指针截断从''void *''到
''int''因为我没有明确地从void *转换为(datatype *)。


我相信这是违反约束的行为,即错误。还有

无法保证,即使你投出的值实际上也适合int。

指针存储为整数(是吗?),


错了。指针存储为指针。这些可以与

整数相同,更大或更小。指向连续位置的指针也可以,

转换为合适的整数类型,而不是连续的整数。

所以作为AFAIK,有什么可担心的在那儿?


有一切值得担心。你的程序错误,不便携,

,一旦你尝试编译64位

目标,就很可能会中断。在那里你可以找到(取决于所使用的模型)指针

是64位宽,而int只有32位宽,并且没有

保证* any *整数类型足够宽。

我正在使用VC 7.1

为什么这些赋值被视为错误?
Hi,

I''m getting a compiler error of "pointer truncation from ''void *'' to
''int''" because I''m not explicitly casting from void* to (datatype*).
That, I believe, is a constraint violation, i.e. an error. There is also
no guarantee that even if you cast the value it will actually fit in an int.
Pointers are stored as ints (is that right?),
Wrong. Pointer are stored as pointers. These could be the same size as
ints, larger or smaller. Pointers to consecutive locations could also,
which converted to a suitable integer type, not be consecutive integers.
so as AFAIK, there is
nothing to worry about is there?
There is everything to worry about. Your program is wrong, non-portable,
and quite likely to break as soon as you try to compile for a 64 bit
target. There you could find (depending on the model used) that pointers
are 64 bits wide whilst int is only 32 bits wide, and there is no
guarantee that *any* integer type is wide enough.
I''m using VC 7.1

Why are such assignments being treated as errors?




因为它们是错误的。无论你想做什么,你几乎都是b $ b肯定做错了。

-

Flash Gordon

生活在有趣的时代。

虽然我的电子邮件地址说垃圾邮件,但这是真的,我读了它。



Because they are errors. Whatever you are trying to do, you are almost
certainly doing it the wrong way.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.


这篇关于指针从'void *'截断到'int'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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