Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

If 2 programs are running, and one program stores a number at a memory address, and if I know that memory address, and hard code it into the 2nd program and print out the value at the address, would it actually get that info? Does C++ allow programs to access any data stored in RAM no matter if it is part of the program or not?

like image 952
Assorted Nuggets Avatar asked Dec 18 '14 04:12

Assorted Nuggets


People also ask

How do you access the memory address of a variable?

Usually memory addresses are represented in hexadecimal. In c++ you can get the memory address of a variable by using the & operator, like: cout << &i << endl; The output of that cout is the memory address of the first byte of the variable i we just created.

Can a process write data anywhere in its address space?

TL;DR No, it's not allowed. In your case, when you got a valid non-NULL pointer to a memory address returned by malloc() , only the requested size of memory is allocated to your process and you're allowed to use (read and / or write) into that much space only.

How is memory accessed in C?

When a variable is created in C, a memory address is assigned to the variable. The memory address is the location of where the variable is stored on the computer. When we assign a value to the variable, it is stored in this memory address.

How can we access memory using pointers?

By assigning a structure to a pointer (for example *MyPointer. Point) it allows to access any memory address in a structured way (with the operator '\'). Pointers allow to move, to read and to write easily in memory.


2 Answers

On system with no virtual memory management and no address space protection this would work. It would be undefined behavior from the point of view of the C standard, but it would produce the behavior that you expect.

Bad news is that most computer systems in use these days have both virtual memory management and address space protection. What this means is that a memory address, the number that your program sees, is not unique in the system. Every process in the system may see the same address, but it would be mapped to a different physical address on your computer at any given moment in time. The operating system and the hardware will create illusion to each process that it has the control of that memory address, while in fact the memory spaces of the processes would not overlap.

Good news is that modern operating systems support some form of shared memory access, meaning that one process can share a segment of memory with other processes, and exchange data by reading and writing the data into that shared segment.

like image 174
Sergey Kalinichenko Avatar answered Nov 15 '22 01:11

Sergey Kalinichenko


No, you'd get a Segmentation Fault

If I try to run this code:

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

I'd get a segmentation fault (unless 0x1234 has been allocated by the process for some reason), which is the operating system's way of telling you that you're not allowed to do that. Usually they'll happen when you're doing tricky things with pointers, but they can also happen elsewhere.

By default, they'll terminate your program immediately unless you're running in a debugger or have registered a signal handler to continue your program

Edit: If you really want, there's ways to get the operating system to let you do that, used by debuggers and such.

like image 36
Xenotoad Avatar answered Nov 14 '22 23:11

Xenotoad