直接指针分配 [英] Direct Pointer assignment

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

问题描述

您好,

我有一小段代码,我想直接为指针分配一段

内存。但是编者告诉我错了。请帮助。下面的代码:

------------------

int * p;

p = 0x241ff5c;

-------------------

Hello,
I have a small piece of code that I want to directly assign a segment of
memory to a pointer. But complier tell me wrong. Pls you help.Codes below:
------------------
int * p;
p=0x241ff5c;
-------------------

推荐答案

在文章< pn ******************* @ wagner.videotron.net>,

陈树生< ly * ****** @ hotmail.tomwrote:
In article <pn*******************@wagner.videotron.net>,
Chen Shusheng <ly*******@hotmail.tomwrote:

>我有一小段代码,我想直接分配一段
内存指针。但是编者告诉我错了。请帮助。下面的代码:
>I have a small piece of code that I want to directly assign a segment of
memory to a pointer. But complier tell me wrong. Pls you help.Codes below:


int * p;

p = 0x241ff5c;
int * p;
p=0x241ff5c;



将整数转换为指针的结果是实现

定义,所以你提出的是不可移植的。如果您的实现记录了行为

,那么您可以在

特定于您的实现的非可移植代码中使用它,根据

记录的功能。该功能存在时,肯定不是线性的 - 例如,为了创建一个指针

到任意内存位置0x241ff5c,你重视你的分配可能

必须是(例如)0x003bff5c8241UL。标准

基本上只是说允许实现创建他们想要的任何

映射,包括不允许任何映射。


在任何情况下,为了让0x241ff5c处理*以某种方式*进入

-some-指针或其他,在你的例子中你会使用,


int * p;

p =(int *)0x241ff5c;


或者更简单地说,


int * p = (int *)0x241ff5c;

-

一切都是虚荣。 - Ecclesiastes

The result of converting an integer to a pointer is implementation
defined, so what you are proposing is not portable. If the behaviour
is documented for your implementation, then you can use it in
non-portable code specific to your implementation, according to the
documented functionality. The functionality, when it exists, is not
certain to be linear -- for example, in order to create a pointer
to arbitrary memory location 0x241ff5c, you value you assign might
have to be something like (say) 0x003bff5c8241UL. The standard
basically just says that implementations are allowed to create any
mapping they want, including not allowing any mapping.

In any case, in order to have 0x241ff5c processed *somehow* into
-some- pointer or other, in your example you would use,

int *p;
p = (int *)0x241ff5c;

Or, more simply,

int *p = (int *)0x241ff5c;
--
All is vanity. -- Ecclesiastes


On Sun,2006年8月20日09:55:06 +0800,陈树生

< ly **在comp.lang.c中*****@hotmail.tomwrote:
On Sun, 20 Aug 2006 09:55:06 +0800, "Chen Shusheng"
<ly*******@hotmail.tomwrote in comp.lang.c:

你好,

我有一小段代码我想直接为指针分配一段

内存。但是编者告诉我错了。请帮助。下面的代码:

------------------

int * p;

p = 0x241ff5c;

-------------------
Hello,
I have a small piece of code that I want to directly assign a segment of
memory to a pointer. But complier tell me wrong. Pls you help.Codes below:
------------------
int * p;
p=0x241ff5c;
-------------------



为什么?那个内存地址是什么?


C允许将整数类型的值赋给任何指针类型,

并使用合适的转换:


int * p =(int *)0x2411ff5c;


结果是实现定义的,可能没有正确的

对齐。实际解除引用这样一个指针的结果是

完全未定义。


-

Jack Klein

主页: http://JK-Technology.Com

常见问题解答

comp.lang.c http:// c-faq .com /

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt.comp.lang.learn.c-c ++
http://www.contrib.andrew.cmu.edu /〜a...FAQ-acllc.html

Why? What''s at that memory address?

C allows a value of integer type to be assigned to any pointer type,
with a suitable cast:

int *p = (int *)0x2411ff5c;

The result is implementation-defined, and might not have the proper
alignment. The result of actually dereferencing such a pointer is
completely undefined.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


我有两个程序。在程序1中,我为该addr分配了一个值。运行
后,程序1关闭。然后我用这段代码运行program2。所有我想要的是
要做的是看程序1是否为addr赋值,它是否会保留在那里

并且程序2可以读取该值。这就是目的。那你们全部为

帮忙。结果是否。内存将被释放。


" Jack Klein" < ja ******* @ spamcop.net>

??????:3j ******************* *************@4ax.com。 ..
I have two programs. In program 1, I assign a value to that addr. After
running, program 1 is closed. Then I run program2 with this code. All I want
to do is to see if program 1 assign a value to a addr, will it remains there
and can program 2 read that value. That is the purpose. That you all for
help. The result is No. The memory will be released.

"Jack Klein" <ja*******@spamcop.net>
??????:3j********************************@4ax.com. ..

On Sun,2006年8月20日09:55:06 +0800,陈树生

< ly ** comp@lang.c中的*****@hotmail.tomwrote:
On Sun, 20 Aug 2006 09:55:06 +0800, "Chen Shusheng"
<ly*******@hotmail.tomwrote in comp.lang.c:

>您好,我有一小段我想要的代码直接将一段内存分配给指针。但是编者告诉我错了。请你帮忙。编码
下面:
------------------
int * p;
p = 0x241ff5c;
-------------------
>Hello,
I have a small piece of code that I want to directly assign a segment of
memory to a pointer. But complier tell me wrong. Pls you help.Codes
below:
------------------
int * p;
p=0x241ff5c;
-------------------



为什么?那个内存地址是什么?


C允许将整数类型的值赋给任何指针类型,

并使用合适的转换:


int * p =(int *)0x2411ff5c;


结果是实现定义的,可能没有正确的

对齐。实际解除引用这样一个指针的结果是

完全未定义。


-

Jack Klein

主页: http://JK-Technology.Com

常见问题解答

comp.lang.c http:// c-faq .com /

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt.comp.lang.learn.c-c ++
http://www.contrib.andrew.cmu.edu /〜a...FAQ-acllc.html



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

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