从int转换为void *并返回int [英] casting from int to void* and back to int

查看:874
本文介绍了从int转换为void *并返回int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


如果以前提到这里,我道歉。

通过新闻组搜索我找到了
$的变种b $ b我的问题,但不完全是我想的。


如果在给定的平台上,我保证:

sizeof(整数) < = sizeof(void *)

将int存储在void *中是否安全并将其转换为int
而不截断/丢失数据?


其次,是标准中定义的void *(或char *),

的最小大小,还是完全平台

依赖?


TIA,

Pranab

Hi All,

I apologize if this has been brought up here before.
Searching through the newsgroup I found variants of
my question, but not exactly what I am thinking about.

If on a given platform, I am guaranteed that:
sizeof(integer) <= sizeof(void*)
Is it safe to store an int in a void* and cast it
back to int without truncating/losing data ?

Secondly, is the minimum size of a void* (or char*),
defined in the standards or is it entirely platform
dependant ?

TIA,
Pranab

推荐答案

Pranab Mehta写道:
Pranab Mehta wrote:
如果在给定的平台上,我保证:
sizeof(整数)< = sizeof(void *)
将int存储在void中是否安全*并将其转换回int而不截断/丢失数据?


这是不安全的,因为标准中没有保证它将会起作用。 C99定义了类型intptr_t。和uintptr_t哪个确实

保持往返转换的平等,指针指向无效,但不幸的是,这是一个错误的行程:* * $>整数 - > void *

而不是整数 - > void * - >整数。


如果int不大于指向void的指针,那么存储

值的一种方法是简单地复制字节:


int i = value;

void * p;

memcpy(& p,& i,sizeof i);


如果稍后将字节复制回int对象,则值为

保证与原始对象相同。这个方法有一个

(相当大的)缺点,你不能使用指针void作为

值,即使是琐碎的表达式声明


p;


如果表示不是有效的,则调用未定义的行为

指向void的指针。这意味着

将指针传递给函数的void也不是严格安全的。


在指针中存储一个int在我看来,无效是一件非常讨厌的事情,并且

表示您的设计可能存在问题。

其次,是虚空的最小尺寸*(或char *),
在标准中定义还是完全依赖于平台?
If on a given platform, I am guaranteed that:
sizeof(integer) <= sizeof(void*)
Is it safe to store an int in a void* and cast it
back to int without truncating/losing data ?
It''s not safe, in that there''s no guarantee in the standard that it
will work. C99 defines the types "intptr_t" and "uintptr_t" which do
preserve equality on round-trip conversions with pointer to void, but
unfortunately the trip is the wrong way: void* -> integer -> void*
rather than integer -> void* -> integer.

If int is no larger than pointer to void then one way to store the
value is by simply copying the bytes:

int i = value;
void *p;
memcpy(&p, &i, sizeof i);

If you later copy the bytes back into an int object, the value is
guaranteed to be the same as the original. This method has the
(rather major) drawback that you can''t use the pointer to void as a
value, even the trivial expression statement

p;

invokes undefined behaviour if the representation is not a valid one
for pointer to void. This means that it''s also not strictly safe to
pass the pointer to void to functions, for example.

Storing an int in a pointer to void is a pretty nasty thing to do, and
indicates a possible problem with your design, in my opinion.
Secondly, is the minimum size of a void* (or char*),
defined in the standards or is it entirely platform
dependant ?




在C99托管环境中,指向void的指针必须是至少17位

宽。


Jeremy。



In a C99 hosted environment a pointer to void must be at least 17 bits
wide.

Jeremy.


Pranab Mehta写道:
Pranab Mehta wrote:

大家好,

我很抱歉,如果以前提到这里的话。
通过新闻组搜索我找到了我的问题的变种,但不完全是我的想法。

如果在给定的平台上,我保证:
sizeof(整数)< = sizeof(void *)
是吗安全地将int存储在void *和把它转回到int而不截断/丢失数据?


这样安全吗?是的,因为在绝大多数的

C实现中,它将按您的意愿工作。它是完全安全吗?       不,因为C语言标准

没有指定任何转换的结果,因此它可能是某些C实现可能是

没有给出理想的结果。


你必须自己决定你的申请需要多少安全



其次,是标准中定义的void *(或char *)的最小大小,还是完全依赖于平台?

Hi All,

I apologize if this has been brought up here before.
Searching through the newsgroup I found variants of
my question, but not exactly what I am thinking about.

If on a given platform, I am guaranteed that:
sizeof(integer) <= sizeof(void*)
Is it safe to store an int in a void* and cast it
back to int without truncating/losing data ?
Is it "safe?" Yes, because on the great majority of
C implementations it will work as you desire. Is it
"perfectly safe?" No, because the C language Standard
does not specify the result of either conversion, and it
is therefore possible that some C implementation might
not give the desired result.

You must make your own decisions about how much "safety"
your applicaition requires.
Secondly, is the minimum size of a void* (or char*),
defined in the standards or is it entirely platform
dependant ?




我不知道你在这里问的是什么。没有C对象

的'sizeof''可以小于1.没有非聚合

对象需要'sizeof''大于1。 />
所有标准必须说明你的问题是


1< = sizeof(void *)&& sizeof(void *)== sizeof(char *)


....每个实现都可以自由选择任何值

满足这些constratints。


-
Er ********* @ sun.com


2004年4月12日13:32:48 -0700, pr **** @ employees.org (Pranab Mehta)写道:
On 12 Apr 2004 13:32:48 -0700, pr****@employees.org (Pranab Mehta) wrote:
大家好,

我为此道歉之前在这里被提到过。
通过新闻组搜索我找到了我的问题的变体,但不完全是我在想什么。

如果在给定的平台上,我是保证:
sizeof(整数)< = sizeof(void *)
将int存储在void *中并将其转换回int而不截断/丢失数据是否安全?
Hi All,

I apologize if this has been brought up here before.
Searching through the newsgroup I found variants of
my question, but not exactly what I am thinking about.

If on a given platform, I am guaranteed that:
sizeof(integer) <= sizeof(void*)
Is it safe to store an int in a void* and cast it
back to int without truncating/losing data ?




我很想成为第一个试图回答这个问题的人,我很高兴

我等了; - )

无论如何,杰里米提出了这个问题在第一次阅读你的帖子后,立即突然出现在我的

心中的事情:为什么?

尽管我想到使用工会,我还是皱起了眉头,在这个

的特殊情况下,我想到了你提出的建议(我的耳朵开始摆动)时,我的鼻子翘得更高。如果使用工会可能会让你失去这样一个丑陋的演员的耻辱吗?

-leor


-

Leor Zolman --- BD软件--- www.bdsoft.com

C / C ++,Java,Perl和Unix的现场培训

C ++用户:下载BD Software的免费STL错误消息解密器: br /> www.bdsoft.com/tools/stlfilt.html


这篇关于从int转换为void *并返回int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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