Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 24th, 2006, 11:09 AM   #21
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
1. You can read and write memory (for the most part) memory in your own process's address space. However, accessing memory not in your process's address space will give a protection fault (or equivalent). The OS will try to handle this fault (For example, if the OS swaps a chunk of memory to disk and unmaps it from your address space, it will reload the chunk of memory and execution of your process can continue).

2. The "address space" term is important to understand. In the x86 architecture at least, the OS will store a table in memory for the processor that more or less says that the section of memory X is actually the section of memory Y. These tables are per-process, as in address A for process A is actually B, but address A for process C is actually D. The result of that is if you try to access address A in process A and expect to get something from process C, you are sadly mistaken. And you will prolly crash if you try that on process E, with address A not mapped to anything...

3. However, whatever is running in "kernel" mode uses real addresses. I'm sure you can guess why.

4. As such, if you want to access memory from another process, go through the kernel. Don't write a device driver or anything crazy. There are debugging facilities built in to most OSes that allow you to do all sorts of wonderful things to other processes. On Windows, take a look at WriteProcessMemory
Note that this doesn't cirumvent the concept of protecting programs from each other at all...execution of such API calls is entirely up to the kernel. If it decides that you don't have permission to fiddle with other processes, then you don't.

5. At this point, your questions haven't even led me to believe you are asking specifically about inter-process memory access or the dangers of accessing random addresses in your own address space. That was assumed. In the very specific instance that you are doing this as an exercise to understand pointers and memory, then yes, that's how you would assign a pointer to an arbitrary address. Yes, if the memory is mapped to your address space, you will probably be able to use that pointer. But mind you, in the real world, whether or not you are running in user-mode, whether or not there's an OS at all, that's not great practice on accessing memory.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270
Dameon is offline   Reply With Quote
Old Apr 29th, 2006, 2:06 AM   #22
lectricpharaoh
SEXY SHOELESS GOD OF WAR!
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,193
Rep Power: 5 lectricpharaoh will become famous soon enough
Quote:
Originally Posted by heapwalker
wow so many words you type. soo many words that i could see you answer confirming peeking into pc memory is inappropriate use that c wasn't design for. could you confirm that c wasn't designed for this use?
Again, you're missing the point. With a modern machine, running a modern OS, much of your direct access to hardware (including memory) is either a) denied, b) virtualized, or when speaking of memory, c) remapped in some way. When a program accesses memory location X, in all likelihood, this will be a different physical memory location than when another program accesses memory location X. The oversimplified answer is that each program operates in its own address space, with memory locations being offsets into this address space. In order to get at other locations (like reading another program's code/data space), you need to employ specific techniques, which vary depending on the machine and OS in use. Likewise, if you want to access fixed physical memory locations (for example, to write to your display adapter's RAM), you need to use different techniques, again specific to your machine and OS.

Both C and C++ provide the capacity to access memory by using addresses, but then, so do many other languages, like BASIC and assembly, but as described, this is not the issue. To put it another way, what you are talking about doing is in the domain of operating system code (kernel and device drivers), or possibly in the domain of debuggers (since these generally need access to another process's address space). It is not in the domain of application programs, and if you keep looking at it that way, you're just going to be banging your head against the wall.

[edit] Perhaps you could tell us what it is you're trying to accomplish. It might well be that you do not need to access the address space of other processes. [/edit]
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:11 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC