Writing a value to a specific memory address

J

Jeong-Gun Lee

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






--
 
B

Burne C

Jeong-Gun Lee said:
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 ?
 
J

Jeong-Gun Lee

Burne C said:
Jeong-Gun Lee said:
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.



Thanks
 
J

Jeong-Gun Lee

Joona I Palaste said:
Jeong-Gun Lee said:
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
 
J

Joona I Palaste

Jeong-Gun Lee said:
Joona I Palaste said:
Jeong-Gun Lee said:
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 ([email protected]) ---------------------------\
| 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
 
R

Randy Howard

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.
 

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
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top