Can I access random data with arbitrary memory Addresses outside of my C ++ program

If there are 2 programs running and one program stores the number at the memory address, and if I know that memory address and hardcode it into the 2nd program and print the value at the address, does it really get Information? Does C ++ programs provide access to any data stored in RAM, whether it is part of the program or not?

+3


source to share


2 answers


On a system without virtual memory management and without address space protection, this will work. This would be undefined behavior in terms of the C standard, but it would create the behavior you expect.

The bad news is that most of the computer systems in use these days have both virtual memory management and address space protection. This means that the memory address, the number that your program sees, is not unique in the system. Every process on the system can see the same address, but at any given time it will appear at a different physical address on your computer. The operating system and hardware will create the illusion for each process that it will control this memory address, when in fact the process memory spaces will not overlap.



The good news is that modern operating systems support some form of shared memory access, which means that one process can share a segment of memory with other processes and exchange data by reading and writing data to that shared segment.

+7


source


No you will get Segmentation Fault

If I try to run this code:

int main(int argc, char *argv[]) {
    int *ptr = (int*) 0x1234;
    *ptr = 10;
}

      



I would get a segmentation fault (if for some reason process 0x1234 was allocated by a process), which is the operating system's way of telling you that you are not allowed to do this. They usually happen when you do complex things with pointers, but they can also happen elsewhere.

By default, they will terminate your program immediately unless you are running in the debugger or register a signal handler to continue your program.

Edit: if you really want it, there are ways to get the operating system to do it using debuggers etc.

+5


source







All Articles