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

前端 未结 2 1325
攒了一身酷
攒了一身酷 2021-01-18 15:29

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 va

2条回答
  •  执笔经年
    2021-01-18 15:52

    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.

提交回复
热议问题