问题描述
如果有2个程序正在运行,并且有一个程序在内存地址中存储一个数字,并且如果我知道该内存地址,然后将其硬编码到第二个程序中并在该地址上打印出该值,那么它会得到信息? C ++是否允许程序访问RAM中存储的任何数据,无论它是否是程序的一部分?
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?
推荐答案
在没有虚拟内存管理且没有地址空间保护的系统上,这将起作用.从C标准的角度来看,这将是未定义的行为,但会产生您期望的行为.
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.
这篇关于我可以在我的C ++程序之外使用随机存储器地址访问随机数据吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!