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

Writing a value to a specific memory address

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a

"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

P: n/a

"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

P: n/a

"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

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

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