printf查询
当我们打印变量的地址时,会打印哪个地址?
如果是虚拟内存,那为什么会这样呢?
任何人都可以解释更多吗...
when we print an address of a variable, which address gets printed?
if it is virtual memory, then why is it so?
can any one explain some more...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在现代桌面/服务器操作系统上,所有内存都是虚拟内存。我不知道有什么方法可以从内核外部访问底层物理地址。即使有可能,它在绝大多数情况下也没有用处。
因此,如果您执行
printf("%p", (void*)&variable);
,它将打印当前进程的变量的虚拟地址。On modern desktop/server OSs, all memory is virtual memory. I'm not aware of any way to access the underlying physical addresses from outside of the kernel. Even if it is possible, it's not going to be useful in the vast majority of situations.
So, if you do
printf("%p", (void*)&variable);
it will print the virtual address of variable for the current process.虚拟内存地址被打印出来,之所以如此,是因为您不需要物理地址,操作系统的全部目的是防止您必须处理物理地址(当然不仅如此,而且也是这样:D)。
The virtual memory address gets printed, and it is so because you don't have any need for the physical address, and the whole point of the OS is to prevent you from having to deal with physical addresses (well it's not only that, but it's also that :D).
在普通的 PC 计算机上,它是将 poitner 转换为相同大小的整数得到的值。
内存地址是虚拟的,当然是的,因为这就是执行代码的进程对计算机中的内存进行寻址的方式。该进程看不到物理内存。
On a normal PC computer, it is a value which you get if you convert poitner to integer of the same size.
Memory address is virtual, yes of course, because that's how the process executing your code addresses the memory in your comupter. The process cannot see physical memory.