By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,421 Members | 1,089 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,421 IT Pros & Developers. It's quick & easy.

in C how to store a value on a given memory address

P: n/a
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!!

Dec 14 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Ashish said:
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!!


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)
Dec 14 '05 #2

P: n/a
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!!

Dec 14 '05 #3

P: n/a
On 14 Dec 2005 02:36:07 -0800, in comp.lang.c , "Ashish"
<ag************@gmail.com> wrote:
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


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 =----
Dec 14 '05 #4

P: n/a
Ashish wrote:
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!!


What is it you are actually trying to?

Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Dec 14 '05 #5

P: n/a
Ashish a écrit :
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!!


There is no standard answer to this question. It's completely a
system-dependent question.

What exactly is your goal ?

--
A+

Emmanuel Delahaye
Dec 14 '05 #6

P: n/a
Ashish a écrit :
i got the solution ..... i wrote this code in VC++ 5.0

int *p = (int *)0x100000UL;
*p = 42;
I guess that your system is Windows. Some old versions could allow that,
but dont that on a NT based Windows (20000, XP).
it compiles correctly but at runtime it is giving error as expression
*p=42 cannot be evaluated .....
Big surprise !
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


On a 'Managed' system, the memory is virtual. The addresses are just
arbitrary numbers having no relationship with the physical memory at all.

What you want to do is (was?) only possible on very small systems, based
on very simple micro processors.

Nowadays, I work with Embedded Linux running on an MPC885 (a PowerPC
Micro-controller), and trust me, there is no way out of the 'Modules'
running in kernel mode that makes the address translation.

--
A+

Emmanuel Delahaye
Dec 14 '05 #7

P: n/a
Emmanuel Delahaye wrote:
....
Nowadays, I work with Embedded Linux running on an MPC885 (a PowerPC
Micro-controller), and trust me, there is no way out of the 'Modules'
running in kernel mode that makes the address translation.


Hi Emmanuel
Off topic, but..
I'd like to get into embedded linux. Can you suggest any URLs or books that
would be good for an experienced programmer to get into hacking embedded linux?
Thanks!
Andrew
Dec 15 '05 #8

P: n/a
hi thanks for the input... and help!!...
this was my first post to the group and got a lot of help.... thanks
again....
actually i was writting a code(firmware) in which i want to store
something on a particular memory location on the flash memory in a
printer(dotmatrix) to test some condition.
i was just checking the code on my machine(windows 2000) first that
this is possible or not... i take an arbitary address 0X100000 but it
does'nt work (as i think it is an operating system concept)... so i
got struk that wether it can be done or not.....

Dec 15 '05 #9

P: n/a
hi thanks for the input... and help!!...
this was my first post to the group and got a lot of help.... thanks
again....
actually i was writting a code(firmware) in which i want to store
something on a particular memory location on the flash memory in a
printer(dotmatrix) to test some condition.
i was just checking the code on my machine(windows 2000) first that
this is possible or not... i take an arbitary address 0X100000 but it
does'nt work (as i think it is an operating system concept)... so i
got struk that wether it can be done or not.....

Dec 15 '05 #10

P: n/a
Andrew R a écrit :
I'd like to get into embedded linux. Can you suggest any URLs or books that
would be good for an experienced programmer to get into hacking embedded linux?


You will have better answers in

news:comp.arch.embedded or in a Linux forum.

--
A+

Emmanuel Delahaye
Dec 16 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.