简体   繁体   English

打印C指针的物理地址

[英]Printing the physical address of a C pointer

I can print the address of a pointer by using the following code 我可以使用以下代码打印指针的地址

#include<stdio.
int main(){
    int *q =(int*) malloc(4);
    printf("%x\n",q);
}

When i execute the following code a hexadecimal value is printed. 当我执行以下代码时,将打印一个十六进制值。 Is it the virtual address or the physical address of variable q on heap? 它是堆上变量q的虚拟地址还是物理地址? If it is virtual how should i print the other? 如果是虚拟的,我该如何打印其他的?

It's in terms of virtual address space. 就虚拟地址空间而言。 It's impossible to get a "physical" address in standard C (unless you're on a system which doesn't have virtual memory, of course, in which case it'll always be the physical address) - if it's currently in swap space, for instance, it may not even have one, other than a current location on disk, which wouldn't be very useful to you. 如果当前在交换空间中,则不可能在标准C中获得“物理”地址(除非您所在的系统没有虚拟内存,否则在这种情况下,它将始终是物理地址) ,例如,除了磁盘上的当前位置以外,它可能甚至没有一个位置,这对您不是很有用。

There is no way of the program knowing whether it is a virtual or physical address. 程序无法知道它是虚拟地址还是物理地址。 The memory is handled by the operating system and MMU which hands out addresses to the program. 内存由操作系统和MMU处理,后者将地址分配给程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM