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

A

Ashish

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!!
 
R

Richard Heathfield

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.
 
A

Ashish

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!!
 
M

Mark McIntyre

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.
 
D

Default User

Ashish said:
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
 
E

Emmanuel Delahaye

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 ?
 
E

Emmanuel Delahaye

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

Andrew R

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
 
A

Ashish

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.....
 
A

Ashish

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.....
 
E

Emmanuel Delahaye

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

or in a Linux forum.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,474
Latest member
AntoniaDea

Latest Threads

Top