473,327 Members | 1,967 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 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 45530

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Kristian Nybo | last post by:
Hi, I'm writing a simple image file exporter as part of a school project. To implement my image format of choice I need to work with big-endian bytes, where 'byte' of course means '8 bits', not...
15
by: Douglas Garstang | last post by:
All, I posted a newsgroup question here a few weeks back, asking some questions that related to my 10 year quest (so far) to understand pointers. Someone suggested I write a simple emulator....
35
by: hasho | last post by:
Why is "call by address" faster than "call by value"?
6
by: hpy_awad | last post by:
I am writing stings ((*cust).name),((*cust).address)to a file using fgets but rabish is being wrote to that file ? Look to my source please and help me finding the reason why this rabish is being...
4
by: Jens Mittag | last post by:
Hi! In my code, I have an array of a structure, which I want to save to a binary file. When the array is just created, everything works fine, but when I change contents of the array, saving...
18
by: ReaperUnreal | last post by:
I've got a strange problem here. I need to take a value that I have written down on a sticky-note here, and find what's at that address in current memory. I've tried the following: void *value =...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
1
by: dodo1267 | last post by:
hi all i need a help in writing cpu simulator i will thank everybody who will guide me how to start doing that and explain this: Program conditionals (loops, if-then-else) are translated into...
6
by: dtschoepe | last post by:
Hi all, Working on homework again... I've got a weird problem, I've been banging my head against the wall on what is causing it. I have a pointer to a typdef named Person. At one point in the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.