Writing a value to a specific memory address | Bytes (2024)

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

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

Subscribe Reply

5 Writing a value to a specific memory address | Bytes (1) 45675 Writing a value to a specific memory address | Bytes (2)

"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*)0x12ff7 c = 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

Jeong-Gun Lee

"Joona I Palaste" <pa*****@cc.hel sinki.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.hel sinki.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

Jeong-Gun Lee

"Burne C" <a@b.com> wrote in message
news:bf******** *@imsp212.netvi gator.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*)0x12ff7 c = 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 itis 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

Joona I Palaste

Jeong-Gun Lee <eu***@kjist.ac .kr> scribbled the following:

"Joona I Palaste" <pa*****@cc.hel sinki.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.progr ammer 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.hel sinki.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

Randy Howard

In article <bf**********@n ews.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.d evelopment.syst em 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 3278

Manipulating and writing bytes (8 bits) in ANSI/ISO C++?

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 'sizeof(char)'. It seems that I could use bitset<8> to represent a byte in my code --- if you have a better suggestion, I welcome it --- but that still leaves me with the question of how to write those bitsets to an image file as big-endian bytes...

C / C++

15 7739

Writing an Emulator

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. Part of his post is below. I would have emailed him directly but a valid email wasn't included.

C / C++

35 10771

call by address vs. call by value

by: hasho |last post by:

Why is "call by address" faster than "call by value"?

C / C++

6 3491

writing strings instead of rabish to file using fprintf

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 written. /* Book name : File name : E:\programs\cpp\iti01\ch10\ex09_5p1.cpp Program discription: Adding name,Address to customer_record SETUP PROGRAM

C / C++

4 5629

trouble, writing an array of structure to binary file.

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 results in a file, which doesn't hold the information of the changed array anymore. I dont know why. Here is the code: /*

C / C++

18 20561

Convert Int Value To Pointer

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 = 0x00323c68; but I get a typecasting error. If I do the following: void *value = static_cast<void *>(0x00323c68);

C / C++

6 5260

problem reading/writing structures from and to files

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;

C / C++

1 3343

help in writing cpu simulator

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 a different family of assembly instructions called branch instructions. Branch instructions check some simple condition, and if the condition is true the next instruction executed by the CPU will not be the following instruction in the code, but...

Java

6 9880

pointer changes value between calls, why/how?

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 code the pointer changes from storing the memory location of the Person and contains the value '1'. I have commented out several pieces of code in order to try to

C / C++

8382

What is ONU?

by: marktang |last post by:

ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...

General

8297

Changing the language in Windows 10

by: Hystou |last post by:

Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...

Windows Server

1 8498

The easy way to turn off automatic updates for Windows 10/11

by: Hystou |last post by:

Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...

Windows Server

8600

Discussion: How does Zigbee compare with other wireless protocols in smart home applications?

by: tracyyun |last post by:

Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...

General

1 6162

Access Europe - Using VBA to create a class based on a table - Wed 1 May

by: isladogs |last post by:

The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...

Microsoft Access / VBA

4150

Trying to create a lan-to-lan vpn between two differents networks

by: TSSRALBI |last post by:

Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

Networking - Hardware / Configuration

4300

Windows Forms - .Net 8.0

by: adsilva |last post by:

A Windows Forms form does not have the event Unload, like VB6. What one acts like?

Visual Basic .NET

2 1930

How to add payments to a PHP MySQL app.

by: muto222 |last post by:

How can i add a mobile payment intergratation into php mysql website.

PHP

2 1600

Comprehensive Guide to Website Development in Toronto: Expert Insights from BSMN Consultancy

by: bsmnconsultancy |last post by:

In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

General

Writing a value to a specific memory address | Bytes (2024)

References

Top Articles
Latest Posts
Article information

Author: Reed Wilderman

Last Updated:

Views: 6082

Rating: 4.1 / 5 (52 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Reed Wilderman

Birthday: 1992-06-14

Address: 998 Estell Village, Lake Oscarberg, SD 48713-6877

Phone: +21813267449721

Job: Technology Engineer

Hobby: Swimming, Do it yourself, Beekeeping, Lapidary, Cosplaying, Hiking, Graffiti

Introduction: My name is Reed Wilderman, I am a faithful, bright, lucky, adventurous, lively, rich, vast person who loves writing and wants to share my knowledge and understanding with you.