指针和整数 [英] pointers and integers

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

问题描述

我有一个与指针/整数转换有关的问题。

在阅读了这个新闻组的各种线程后,我发现

指针/整数转换不可移植,可能导致

即使在同一平台上也未定义结果。我只是好奇

知道为什么会这样?一个明显的原因是

指针变量的大小可能与整数变量不一样。

其他原因可能是什么?


考虑一个实现,其中指针和整数变量

都是*相同*大小(比如4个字节)。

在这样的平台上,可以是整数/指针转换原因

任何未定义的结果?为什么?

例如。考虑下面这段代码:


int main(无效)

{

int * ptr_i;

int value = 0x100; / *我想访问位置0x100

内存* /


ptr_i = value;

}


假设sizeof(int *)等于sizeof(int)。

ptr_i = value可能会出现什么问题?可能导致?

转换后的价值到ptr_i,是否可能

ptr_i总共包含一个不同的值?

I have a question related to pointer/integer conversion.
After reading various threads on this newsgroup I found that
pointer/integer conversion is not portable and may lead to
undefined results even on the same platform. I was just curious
to know why is it so ? One obvious reason is that size of
pointer variable might not be the same as integer variable.
What could be the other reasons ?

Consider an implementation where pointers and integer variables
are all of *same* size (say 4 bytes).
On such a platform, can the integer/pointer conversion cause
any undefined results ? Why ?
For eg. Consider the following piece of code:

int main (void)
{
int * ptr_i ;
int value = 0x100; /* I want to access location 0x100 in
memory */

ptr_i = value;
}

Assume sizeof(int *) is equal to sizeof(int).
What sort of problems could the "ptr_i = value" may lead to ?
After the conversion of "value" to "ptr_i", is it possible
that "ptr_i" contain altogether a different value ?

推荐答案

当指针和整数大小相同时你需要

应该能够将指针转换为整数,而不会出现任何问题。


但是由于
$ b,这不是推荐的做法$ b便携性,代码可读性和可靠性问题。


-

EventStudio 2.5 - http://www.EventHelix.com/EventStudio

以纯文本格式输入模型;以PDF和Word格式生成调用流程

When the pointer and the integer are of the same size you
should be able to convert a pointer to an integer without
any problem.

However it is not a recommended practice due to
portability, code readability and reliability concerns.

--
EventStudio 2.5 - http://www.EventHelix.com/EventStudio
Enter model in plain text;generate call flow in PDF and Word


你怎么知道内存位置0x100可供使用。

How do you know memory location 0x100 is available for using.


< a href =mailto:ju ********** @ yahoo.co.in> ju ********** @ yahoo.co.in 写道:

我有一个与指针/整数转换有关的问题。
读完这个newsgro上的各种线程后我发现
指针/整数转换不可移植,即使在同一平台上也可能导致未定义的结果。我很好奇
知道为什么会这样?一个显而易见的原因是指针变量的大小可能与整数变量的大小不同。
其他原因可能是什么?

考虑指针和整数变量的实现
都是*相同*大小(比如4个字节)。
在这样的平台上,整数/指针转换是否会导致任何未定义的结果?为什么?
例如。请考虑以下代码:

int main(void)
{int / ptr_i;
int value = 0x100; / *我想在内存中访问位置0x100 * /

ptr_i = value;
}

I have a question related to pointer/integer conversion.
After reading various threads on this newsgroup I found that
pointer/integer conversion is not portable and may lead to
undefined results even on the same platform. I was just curious
to know why is it so ? One obvious reason is that size of
pointer variable might not be the same as integer variable.
What could be the other reasons ?

Consider an implementation where pointers and integer variables
are all of *same* size (say 4 bytes).
On such a platform, can the integer/pointer conversion cause
any undefined results ? Why ?
For eg. Consider the following piece of code:

int main (void)
{
int * ptr_i ;
int value = 0x100; /* I want to access location 0x100 in
memory */

ptr_i = value;
}




如果你知道你想要在内存中访问位置0x100,

然后你没有处理便携式代码的情况,

所以为什么要担心这些代码的可移植性?


-

pete



If you know that you want to access location 0x100 in memory,
then you''re not dealing with a portable code situation,
so why worry about portability of such code?

--
pete


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

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