![]() |
[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?
|
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. |
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> |
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.
|
All times are GMT -8. The time now is 03:10 AM. |
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project