472,135 Members | 1,279 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,135 software developers and data experts.

Writing a value to a specific memory address

I'm writing a code of writing a value to a specific memory address.

================================================== ===============
#include <stdio.h>

int main()
{
long air;
long *air_address;

printf("Air : %d, Air_address : %p\n", air, air_address);
(long *) air = air_address;
* (long *) air = 1211;

printf("Air : %d\n", * (long *) air);

/* Direct Memory Access to MEM[ 0x40009ca0 ] */
* (long *) 0x40009ca0 = 10;
printf("Direct MEM[0x40009ca0] = %d \n", * (long *) 0x40009ca0);

}

================================================== ===============

It was complied without any errors, but causes a run-time segmentation
error.
Is there anyone who tell the reason of why ?
and how to assign a value to a specific memory address ?

My test machine is a pentium processor and OS is a linux.

Thanks...


--
-------------------------------------------------------------
Jeong-Gun Lee
Concurrent System Research Lab., KJIST, South Korea.
Homepage : http://async.kjist.ac.kr/~eulia
E-mail : eulia(at)kjist.ac.kr, Phone : 062-970-2267, 016-382-6765
Nov 13 '05 #1
5 44753

"Jeong-Gun Lee" <eu***@kjist.ac.kr> wrote in message
news:bf**********@news.kreonet.re.kr...
I'm writing a code of writing a value to a specific memory address.

================================================== ===============
#include <stdio.h>

int main()
{
long air;
long *air_address;

printf("Air : %d, Air_address : %p\n", air, air_address);
(long *) air = air_address;
* (long *) air = 1211;

printf("Air : %d\n", * (long *) air);

/* Direct Memory Access to MEM[ 0x40009ca0 ] */
* (long *) 0x40009ca0 = 10;
printf("Direct MEM[0x40009ca0] = %d \n", * (long *) 0x40009ca0);

}

================================================== ===============

It was complied without any errors, but causes a run-time segmentation
error.
Is there anyone who tell the reason of why ?
and how to assign a value to a specific memory address ?

My test machine is a pentium processor and OS is a linux.

Thanks...


If you have a valuable and get its address, let say

long abc;

printf("The address of abc : %X \n" , (long)&abc);

The output is, for example : 12ff7c

You will find that you can write some value to the memory using this
address,

*(long*)0x12ff7c = 20;

But if you have a function, and you get the address of it and try to assign
a value by the address, you will cause segmentation error, I think it is
because that area of memory is the "code" section of the program, and it is
not "writable".

For the similar reason, are you sure that your address "0x40009ca0" is
writable ?

--
BC


Nov 13 '05 #2

"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
news:bf**********@oravannahka.helsinki.fi...
Jeong-Gun Lee <eu***@kjist.ac.kr> scribbled the following:
I'm writing a code of writing a value to a specific memory address.
================================================== ===============
#include <stdio.h>

int main()
{
long air;
long *air_address;

printf("Air : %d, Air_address : %p\n", air, air_address);
(long *) air = air_address;
* (long *) air = 1211;

printf("Air : %d\n", * (long *) air);

/* Direct Memory Access to MEM[ 0x40009ca0 ] */
* (long *) 0x40009ca0 = 10;
printf("Direct MEM[0x40009ca0] = %d \n", * (long *) 0x40009ca0);

}

================================================== ===============

It was complied without any errors, but causes a run-time segmentation
error.
Is there anyone who tell the reason of why ?
and how to assign a value to a specific memory address ?

My test machine is a pentium processor and OS is a linux.

Thanks...


Writing willy-nilly at hard-coded addresses like that causes
undefined behaviour. The implementation is allowed to do anything it
pleases. On Linux, and other Unices, memory is protected, so writing
to memory you don't own causes a segmentation fault. You just can't
go around writign to hard-coded addresses thinking they belong to
you, when there's a good chance they might not.
What are you trying to achieve in the first place? If you need
memory to write to, malloc() is your friend.

Yes, I know the way of using "malloc" function.
I'm just curious on the direct memory writing at the low level on my linux
PC.
Actually, when writing a program for an MCU accessing a memory bank at the
board level,
my code worked well.

Anyway, is there noway of writing a value to a specific memory in a OS
governing PC using a C language ?

Thanks
--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"'So called' means: 'There is a long explanation for this, but I have no
time to explain it here.'"
- JIPsoft

Nov 13 '05 #3

"Burne C" <a@b.com> wrote in message
news:bf*********@imsp212.netvigator.com...

"Jeong-Gun Lee" <eu***@kjist.ac.kr> wrote in message
news:bf**********@news.kreonet.re.kr...
I'm writing a code of writing a value to a specific memory address.

================================================== ===============
#include <stdio.h>

int main()
{
long air;
long *air_address;

printf("Air : %d, Air_address : %p\n", air, air_address);
(long *) air = air_address;
* (long *) air = 1211;

printf("Air : %d\n", * (long *) air);

/* Direct Memory Access to MEM[ 0x40009ca0 ] */
* (long *) 0x40009ca0 = 10;
printf("Direct MEM[0x40009ca0] = %d \n", * (long *) 0x40009ca0);

}

================================================== ===============

It was complied without any errors, but causes a run-time segmentation
error.
Is there anyone who tell the reason of why ?
and how to assign a value to a specific memory address ?

My test machine is a pentium processor and OS is a linux.

Thanks...

If you have a valuable and get its address, let say

long abc;

printf("The address of abc : %X \n" , (long)&abc);

The output is, for example : 12ff7c

You will find that you can write some value to the memory using this
address,

*(long*)0x12ff7c = 20;

But if you have a function, and you get the address of it and try to

assign a value by the address, you will cause segmentation error, I think it is
because that area of memory is the "code" section of the program, and it is not "writable".

For the similar reason, are you sure that your address "0x40009ca0" is
writable ?
The address "0x40009ca0" is derived in same way as you did !
But its usage caused segmentation error.

From the experience, I think that all the direct memory access at the code
level
is protected. And through only system call in a function like "malloc" could
reserve
the memory.


--
BC

Thanks
Nov 13 '05 #4
Jeong-Gun Lee <eu***@kjist.ac.kr> scribbled the following:
"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
news:bf**********@oravannahka.helsinki.fi...
Jeong-Gun Lee <eu***@kjist.ac.kr> scribbled the following:
> I'm writing a code of writing a value to a specific memory address.
> ================================================== ===============
> #include <stdio.h>

> int main()
> {
> long air;
> long *air_address;

> printf("Air : %d, Air_address : %p\n", air, air_address);
> (long *) air = air_address;
> * (long *) air = 1211;

> printf("Air : %d\n", * (long *) air);

> /* Direct Memory Access to MEM[ 0x40009ca0 ] */
> * (long *) 0x40009ca0 = 10;
> printf("Direct MEM[0x40009ca0] = %d \n", * (long *) 0x40009ca0);

> }

> ================================================== ===============

> It was complied without any errors, but causes a run-time segmentation
> error.
> Is there anyone who tell the reason of why ?
> and how to assign a value to a specific memory address ?

> My test machine is a pentium processor and OS is a linux.

> Thanks...


Writing willy-nilly at hard-coded addresses like that causes
undefined behaviour. The implementation is allowed to do anything it
pleases. On Linux, and other Unices, memory is protected, so writing
to memory you don't own causes a segmentation fault. You just can't
go around writign to hard-coded addresses thinking they belong to
you, when there's a good chance they might not.
What are you trying to achieve in the first place? If you need
memory to write to, malloc() is your friend.

Yes, I know the way of using "malloc" function.
I'm just curious on the direct memory writing at the low level on my linux
PC.
Actually, when writing a program for an MCU accessing a memory bank at the
board level,
my code worked well.
The whole point of the virtual memory system in Linux is that in user-
level code, accesses to memory addresses that don't belong to you cause
segmentation faults. The system is trying to protect itself from
crashing.
If you want to write low-level code in Linux, ask in
comp.unix.programmer or a Linux newsgroup. Such things are outside the
scope of the C programming language.
Anyway, is there noway of writing a value to a specific memory in a OS
governing PC using a C language ?


Not in user mode, at least. Why do you want to do it anyway? Do you
have a special reason to "hit the iron" as it is called? Do you want to
hack at various bits in memory to see if you can get the OS to crash, or
are you a control freak of some sort?
The Linux system usually does a better job at this than you do. If
you're trying to access some specific hardware, chances are there are
already Linux-compatible drivers for it.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"Ice cream sales somehow cause drownings: both happen in summer."
- Antti Voipio & Arto Wikla
Nov 13 '05 #5
In article <bf**********@news.kreonet.re.kr>, eu***@kjist.ac.kr says...
From the experience, I think that all the direct memory access at the code
level is protected.
If it exists outside of the memory allocated to program.
And through only system call in a function like "malloc" could reserve
the memory.


The answer which has already been given to you partly, is that for the
OS in question (Linux) you should be asking this in a linux specific
programming newsgroup. comp.os.linux.development.system might be a
good choice.

You will have to at write your own driver to do what you intend.
Nov 13 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by Douglas Garstang | last post: by
35 posts views Thread by hasho | last post: by
18 posts views Thread by ReaperUnreal | last post: by
6 posts views Thread by arne.muller | last post: by
1 post views Thread by dodo1267 | last post: by
6 posts views Thread by dtschoepe | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.