在C中如何在给定的内存地址上存储一个值 [英] in C how to store a value on a given memory address

查看:62
本文介绍了在C中如何在给定的内存地址上存储一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我是C的新手

我有一个查询....

可以存储给定内存的值位置在C

例如我想在位置存储整数值10

0X100000。

我怎么能在C中这样做..如果有任何身体可以帮助!!

Hi
I am new to C
I have one query....
can i store a value on a given memory location in C
say for example i want to store an integer value 10 at location
0X100000.
how can i do it in C... if any body can help!!

推荐答案

Ashish说:
Ashish said:
你好
我是C的新手
我有一个查询....
我可以在C中的给定内存位置存储一个值
例如我想存储一个整数价值10在位置
0X100000。
我怎么能在C ...如果有任何身体可以帮助!!
Hi
I am new to C
I have one query....
can i store a value on a given memory location in C
say for example i want to store an integer value 10 at location
0X100000.
how can i do it in C... if any body can help!!




C标准并不能保证你能做到这一点,并且在很多

平台上你都做不到。但是,有一些平台可以。在这样的平台上,你可以这样做:


int * p =(int *)0x100000UL;

* p = 42;


但要这样做会调用未定义的行为。它不是一个便携式结构。


在旧的MS-DOS下方便,因为表现不好。攻击

硬件的程序可以绕过它相当笨拙的界面。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)



The C Standard does not guarantee that you can do this, and on many
platforms you can''t. There are, however, some platforms where you can. On
such platforms, you could do this:

int *p = (int *)0x100000UL;
*p = 42;

but to do so invokes undefined behaviour. It is not a portable construct.

Handy under good old MS-DOS, for "badly-behaved" programs that hacked the
hardware to step around its rather ploddy interfaces thereto.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


谢谢

i得到了解决方案.....我在VC ++ 5.0中编写了这段代码


int * p =(int *)0x100000UL;

* p = 42;


它正确编译但在运行时它表示错误表达

* p = 42无法评估.....

i发现只有当内存位置不是由操作系统保留的
并且它应该是内存段(RAM)的一部分时才会运行此代码

其中变量存储....

所以我发现内存位置,它适用于我的地址

0x0012ff92


再次感谢为了你的帮助!!

Thanks
i got the solution ..... i wrote this code in VC++ 5.0

int *p = (int *)0x100000UL;
*p = 42;

it compiles correctly but at runtime it is giving error as expression
*p=42 cannot be evaluated .....
i found that this code will be run only when the memory location is not
reserved by OS and that it should be the part of memory segment (RAM)
where variable is stored....
so i found that memory position and it works for me at address
0x0012ff92

Thanks again for ur help!!


2005年12月14日02:36:07 -0800,在comp.lang.c,Ashish

< ag ************ @ gmail.com>写道:
On 14 Dec 2005 02:36:07 -0800, in comp.lang.c , "Ashish"
<ag************@gmail.com> wrote:
int * p =(int *)0x100000UL;
* p = 42;

它正确编译但在运行时它给出了表达式错误
* p = 42无法评估.....


是的,你不能这样做。操作系统会非常正确地阻止你。你

无法知道你是否拥有该内存位置,所以写入它

是不允许的。

i发现此代码将是仅当内存位置不被OS保留并且应该是存储器段(RAM)的一部分时才运行
存储变量....


即使hte OS允许你这样做,你仍然不知道你是否真的拥有这个记忆。其他一些应用程序(比如你的网卡驱动程序,
或屏幕驱动程序,或者喜欢的编辑器)可能正在使用它。所以这是

仍然不是一个好主意。

所以我发现内存位置,它适用于我的地址
0x0012ff92
int *p = (int *)0x100000UL;
*p = 42;

it compiles correctly but at runtime it is giving error as expression
*p=42 cannot be evaluated .....
Yes, you can''t do this. The OS will quite rightly try to stop you. You
have no way to know if you own that memory location, so writing to it
is disallowed.
i found that this code will be run only when the memory location is not
reserved by OS and that it should be the part of memory segment (RAM)
where variable is stored....
Even if hte OS allowed you do, you still don''t know if you actually
own this memory. Some other application (say your network card driver,
or screen driver, or favorite editor) might be using it. So this is
still not a good idea.
so i found that memory position and it works for me at address
0x0012ff92




明天那也不行。

..

对于任何现代操作系统,程序拥有的内存区域是不是

由您可以选择的整数值定义 - 操作系统决定所有

。你不应该试图直接写入内存。


---- ==通过Newsfeeds.Com发布 - 无限制 - 无限制 - 安全Usenet新闻== ---- http://www.newsfeeds.com 世界排名第一的新闻组服务! 120,000多个新闻组

---- =东海岸和西海岸服务器农场 - 通过加密实现全隐私= ----



And tomorrow that won''t work either.
..
With any modern OS, the regions of memory your programme owns are not
defined by integer values you can pick and choose - the OS decides all
that. You should not attempt to write to memory directly.

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----


这篇关于在C中如何在给定的内存地址上存储一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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