![]() |
![]() |
#1 (permalink) |
Crazy
Location: Raleigh, NC
|
[c] memory locations between apps
I tried to write two programs, one that would set a value to an int and display its memory address to the user, and another one that would print the value found at a user-entered address. I could never get the value I wanted. Is there a reason that a memory address could not be accessed in this way?
__________________
"Good artists copy, great artists steal." - Pablo Picasso |
![]() |
![]() |
#2 (permalink) |
Lover - Protector - Teacher
Location: Seattle, WA
|
Displaying the address of user-entered int should perform as you intend:
int blah; printf("Address of %d is %d", blah, &blah); Note the &. However, you should not be able to do it the other way around. If C or C++ or any language for that matter allowed address control like that, you could crash the entire computer with your program. Here, let me write to protected OS stack memory. A program should never allow "illegal operations" on protected memory, for the sake of the user's computer who is using the machine. In addition, if you're terminating the first program before starting the second, you have absolutely no idea if that memory address is still valid. What if another program currently running stored something there, temporarily? It was freed up when the first exited. And if you didnt terminate the first before starting the second, then a program has control of that memory access and the OS shouldn't let another program access it.
__________________
"I'm typing on a computer of science, which is being sent by science wires to a little science server where you can access it. I'm not typing on a computer of philosophy or religion or whatever other thing you think can be used to understand the universe because they're a poor substitute in the role of understanding the universe which exists independent from ourselves." - Willravel |
![]() |
![]() |
#3 (permalink) |
Junkie
Location: San Francisco
|
On any modern OS, with some exceptions like the embedded single-process variety, every application has its own virtual memory space and it's impossible to access any other application's space without help from the OS (well, technically you need help from the OS to access any memory at all since you never deal with hardware addresses directly, but you need extra-special help for this), which is referred to generally as interprocess communication (IPC).
On windows, you can use the APIs ReadProcessMemory and WriteProcessMemory. Here is a program that reads the first 10 bytes at memory location 0x00400000 of process with ID 666: Code:
#include <windows.h> int main() { char c[10]; void * p = (void*)0x00400000; HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, 666); ReadProcessMemory(hProcess, p, c, 10, NULL); CloseHandle(hProcess); // ok, its a little redundant return 0; } |
![]() |
![]() |
#4 (permalink) |
Crazy
Location: Raleigh, NC
|
Thanks, I did keep the first app running but then I was confused as to why the memory address was always the same on every run. The virtual memory explanation was helpful thanks.
__________________
"Good artists copy, great artists steal." - Pablo Picasso |
![]() |
Tags |
apps, locations, memory |
|
|